2
0

SHA224.as 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.crypto
  28. {
  29. import com.adobe.utils.IntUtil;
  30. import flash.utils.ByteArray;
  31. import mx.utils.Base64Encoder;
  32. /**
  33. * The SHA-224 algorithm
  34. *
  35. * @see http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
  36. */
  37. public class SHA224
  38. {
  39. public static var digest:ByteArray;
  40. /**
  41. * Performs the SHA224 hash algorithm on a string.
  42. *
  43. * @param s The string to hash
  44. * @return A string containing the hash value of s
  45. * @langversion ActionScript 3.0
  46. * @playerversion 9.0
  47. * @tiptext
  48. */
  49. public static function hash( s:String ):String {
  50. var blocks:Array = createBlocksFromString( s );
  51. var byteArray:ByteArray = hashBlocks( blocks );
  52. return IntUtil.toHex( byteArray.readInt(), true )
  53. + IntUtil.toHex( byteArray.readInt(), true )
  54. + IntUtil.toHex( byteArray.readInt(), true )
  55. + IntUtil.toHex( byteArray.readInt(), true )
  56. + IntUtil.toHex( byteArray.readInt(), true )
  57. + IntUtil.toHex( byteArray.readInt(), true )
  58. + IntUtil.toHex( byteArray.readInt(), true );
  59. }
  60. /**
  61. * Performs the SHA224 hash algorithm on a ByteArray.
  62. *
  63. * @param data The ByteArray data to hash
  64. * @return A string containing the hash value of data
  65. * @langversion ActionScript 3.0
  66. * @playerversion 9.0
  67. */
  68. public static function hashBytes( data:ByteArray ):String
  69. {
  70. var blocks:Array = createBlocksFromByteArray( data );
  71. var byteArray:ByteArray = hashBlocks(blocks);
  72. return IntUtil.toHex( byteArray.readInt(), true )
  73. + IntUtil.toHex( byteArray.readInt(), true )
  74. + IntUtil.toHex( byteArray.readInt(), true )
  75. + IntUtil.toHex( byteArray.readInt(), true )
  76. + IntUtil.toHex( byteArray.readInt(), true )
  77. + IntUtil.toHex( byteArray.readInt(), true )
  78. + IntUtil.toHex( byteArray.readInt(), true );
  79. }
  80. /**
  81. * Performs the SHA224 hash algorithm on a string, then does
  82. * Base64 encoding on the result.
  83. *
  84. * @param s The string to hash
  85. * @return The base64 encoded hash value of s
  86. * @langversion ActionScript 3.0
  87. * @playerversion 9.0
  88. * @tiptext
  89. */
  90. public static function hashToBase64( s:String ):String
  91. {
  92. var blocks:Array = createBlocksFromString( s );
  93. var byteArray:ByteArray = hashBlocks(blocks);
  94. // ByteArray.toString() returns the contents as a UTF-8 string,
  95. // which we can't use because certain byte sequences might trigger
  96. // a UTF-8 conversion. Instead, we convert the bytes to characters
  97. // one by one.
  98. var charsInByteArray:String = "";
  99. byteArray.position = 0;
  100. for (var j:int = 0; j < byteArray.length; j++)
  101. {
  102. var byte:uint = byteArray.readUnsignedByte();
  103. charsInByteArray += String.fromCharCode(byte);
  104. }
  105. var encoder:Base64Encoder = new Base64Encoder();
  106. encoder.encode(charsInByteArray);
  107. return encoder.flush();
  108. }
  109. private static function hashBlocks( blocks:Array ):ByteArray {
  110. var h0:int = 0xc1059ed8;
  111. var h1:int = 0x367cd507;
  112. var h2:int = 0x3070dd17;
  113. var h3:int = 0xf70e5939;
  114. var h4:int = 0xffc00b31;
  115. var h5:int = 0x68581511;
  116. var h6:int = 0x64f98fa7;
  117. var h7:int = 0xbefa4fa4;
  118. var k:Array = new Array(0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
  119. var len:int = blocks.length;
  120. var w:Array = new Array();
  121. // loop over all of the blocks
  122. for ( var i:int = 0; i < len; i += 16 ) {
  123. var a:int = h0;
  124. var b:int = h1;
  125. var c:int = h2;
  126. var d:int = h3;
  127. var e:int = h4;
  128. var f:int = h5;
  129. var g:int = h6;
  130. var h:int = h7;
  131. for(var t:int = 0; t < 64; t++) {
  132. if ( t < 16 ) {
  133. w[t] = blocks[ i + t ];
  134. if(isNaN(w[t])) { w[t] = 0; }
  135. } else {
  136. var ws0:int = IntUtil.ror(w[t-15], 7) ^ IntUtil.ror(w[t-15], 18) ^ (w[t-15] >>> 3);
  137. var ws1:int = IntUtil.ror(w[t-2], 17) ^ IntUtil.ror(w[t-2], 19) ^ (w[t-2] >>> 10);
  138. w[t] = w[t-16] + ws0 + w[t-7] + ws1;
  139. }
  140. var s0:int = IntUtil.ror(a, 2) ^ IntUtil.ror(a, 13) ^ IntUtil.ror(a, 22);
  141. var maj:int = (a & b) ^ (a & c) ^ (b & c);
  142. var t2:int = s0 + maj;
  143. var s1:int = IntUtil.ror(e, 6) ^ IntUtil.ror(e, 11) ^ IntUtil.ror(e, 25);
  144. var ch:int = (e & f) ^ ((~e) & g);
  145. var t1:int = h + s1 + ch + k[t] + w[t];
  146. h = g;
  147. g = f;
  148. f = e;
  149. e = d + t1;
  150. d = c;
  151. c = b;
  152. b = a;
  153. a = t1 + t2;
  154. }
  155. //Add this chunk's hash to result so far:
  156. h0 += a;
  157. h1 += b;
  158. h2 += c;
  159. h3 += d;
  160. h4 += e;
  161. h5 += f;
  162. h6 += g;
  163. h7 += h;
  164. }
  165. var byteArray:ByteArray = new ByteArray();
  166. byteArray.writeInt(h0);
  167. byteArray.writeInt(h1);
  168. byteArray.writeInt(h2);
  169. byteArray.writeInt(h3);
  170. byteArray.writeInt(h4);
  171. byteArray.writeInt(h5);
  172. byteArray.writeInt(h6);
  173. byteArray.position = 0;
  174. digest = new ByteArray();
  175. digest.writeBytes(byteArray);
  176. digest.position = 0;
  177. return byteArray;
  178. }
  179. /**
  180. * Converts a ByteArray to a sequence of 16-word blocks
  181. * that we'll do the processing on. Appends padding
  182. * and length in the process.
  183. *
  184. * @param data The data to split into blocks
  185. * @return An array containing the blocks into which data was split
  186. */
  187. private static function createBlocksFromByteArray( data:ByteArray ):Array
  188. {
  189. var oldPosition:int = data.position;
  190. data.position = 0;
  191. var blocks:Array = new Array();
  192. var len:int = data.length * 8;
  193. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  194. for( var i:int = 0; i < len; i += 8 )
  195. {
  196. blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );
  197. }
  198. // append padding and length
  199. blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
  200. blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
  201. data.position = oldPosition;
  202. return blocks;
  203. }
  204. /**
  205. * Converts a string to a sequence of 16-word blocks
  206. * that we'll do the processing on. Appends padding
  207. * and length in the process.
  208. *
  209. * @param s The string to split into blocks
  210. * @return An array containing the blocks that s was split into.
  211. */
  212. private static function createBlocksFromString( s:String ):Array
  213. {
  214. var blocks:Array = new Array();
  215. var len:int = s.length * 8;
  216. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  217. for( var i:int = 0; i < len; i += 8 ) {
  218. blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 );
  219. }
  220. // append padding and length
  221. blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
  222. blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
  223. return blocks;
  224. }
  225. }
  226. }