SHA256.as 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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-256 algorithm
  34. *
  35. * @see http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
  36. */
  37. public class SHA256
  38. {
  39. public static var digest:ByteArray;
  40. /**
  41. * Performs the SHA256 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. + IntUtil.toHex( byteArray.readInt(), true );
  60. }
  61. /**
  62. * Performs the SHA256 hash algorithm on a ByteArray.
  63. *
  64. * @param data The ByteArray data to hash
  65. * @return A string containing the hash value of data
  66. * @langversion ActionScript 3.0
  67. * @playerversion 9.0
  68. */
  69. public static function hashBytes( data:ByteArray ):String
  70. {
  71. var blocks:Array = createBlocksFromByteArray( data );
  72. var byteArray:ByteArray = hashBlocks(blocks);
  73. return 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. + IntUtil.toHex( byteArray.readInt(), true )
  80. + IntUtil.toHex( byteArray.readInt(), true );
  81. }
  82. /**
  83. * Performs the SHA256 hash algorithm on a string, then does
  84. * Base64 encoding on the result.
  85. *
  86. * @param s The string to hash
  87. * @return The base64 encoded hash value of s
  88. * @langversion ActionScript 3.0
  89. * @playerversion 9.0
  90. * @tiptext
  91. */
  92. public static function hashToBase64( s:String ):String
  93. {
  94. var blocks:Array = createBlocksFromString( s );
  95. var byteArray:ByteArray = hashBlocks(blocks);
  96. // ByteArray.toString() returns the contents as a UTF-8 string,
  97. // which we can't use because certain byte sequences might trigger
  98. // a UTF-8 conversion. Instead, we convert the bytes to characters
  99. // one by one.
  100. var charsInByteArray:String = "";
  101. byteArray.position = 0;
  102. for (var j:int = 0; j < byteArray.length; j++)
  103. {
  104. var byte:uint = byteArray.readUnsignedByte();
  105. charsInByteArray += String.fromCharCode(byte);
  106. }
  107. var encoder:Base64Encoder = new Base64Encoder();
  108. encoder.encode(charsInByteArray);
  109. return encoder.flush();
  110. }
  111. private static function hashBlocks( blocks:Array ):ByteArray {
  112. var h0:int = 0x6a09e667;
  113. var h1:int = 0xbb67ae85;
  114. var h2:int = 0x3c6ef372;
  115. var h3:int = 0xa54ff53a;
  116. var h4:int = 0x510e527f;
  117. var h5:int = 0x9b05688c;
  118. var h6:int = 0x1f83d9ab;
  119. var h7:int = 0x5be0cd19;
  120. 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);
  121. var len:int = blocks.length;
  122. var w:Array = new Array( 64 );
  123. // loop over all of the blocks
  124. for ( var i:int = 0; i < len; i += 16 ) {
  125. var a:int = h0;
  126. var b:int = h1;
  127. var c:int = h2;
  128. var d:int = h3;
  129. var e:int = h4;
  130. var f:int = h5;
  131. var g:int = h6;
  132. var h:int = h7;
  133. for(var t:int = 0; t < 64; t++) {
  134. if ( t < 16 ) {
  135. w[t] = blocks[ i + t ];
  136. if(isNaN(w[t])) { w[t] = 0; }
  137. } else {
  138. var ws0:int = IntUtil.ror(w[t-15], 7) ^ IntUtil.ror(w[t-15], 18) ^ (w[t-15] >>> 3);
  139. var ws1:int = IntUtil.ror(w[t-2], 17) ^ IntUtil.ror(w[t-2], 19) ^ (w[t-2] >>> 10);
  140. w[t] = w[t-16] + ws0 + w[t-7] + ws1;
  141. }
  142. var s0:int = IntUtil.ror(a, 2) ^ IntUtil.ror(a, 13) ^ IntUtil.ror(a, 22);
  143. var maj:int = (a & b) ^ (a & c) ^ (b & c);
  144. var t2:int = s0 + maj;
  145. var s1:int = IntUtil.ror(e, 6) ^ IntUtil.ror(e, 11) ^ IntUtil.ror(e, 25);
  146. var ch:int = (e & f) ^ ((~e) & g);
  147. var t1:int = h + s1 + ch + k[t] + w[t];
  148. h = g;
  149. g = f;
  150. f = e;
  151. e = d + t1;
  152. d = c;
  153. c = b;
  154. b = a;
  155. a = t1 + t2;
  156. }
  157. //Add this chunk's hash to result so far:
  158. h0 += a;
  159. h1 += b;
  160. h2 += c;
  161. h3 += d;
  162. h4 += e;
  163. h5 += f;
  164. h6 += g;
  165. h7 += h;
  166. }
  167. var byteArray:ByteArray = new ByteArray();
  168. byteArray.writeInt(h0);
  169. byteArray.writeInt(h1);
  170. byteArray.writeInt(h2);
  171. byteArray.writeInt(h3);
  172. byteArray.writeInt(h4);
  173. byteArray.writeInt(h5);
  174. byteArray.writeInt(h6);
  175. byteArray.writeInt(h7);
  176. byteArray.position = 0;
  177. digest = new ByteArray();
  178. digest.writeBytes(byteArray);
  179. digest.position = 0;
  180. return byteArray;
  181. }
  182. /**
  183. * Converts a ByteArray to a sequence of 16-word blocks
  184. * that we'll do the processing on. Appends padding
  185. * and length in the process.
  186. *
  187. * @param data The data to split into blocks
  188. * @return An array containing the blocks into which data was split
  189. */
  190. private static function createBlocksFromByteArray( data:ByteArray ):Array
  191. {
  192. var oldPosition:int = data.position;
  193. data.position = 0;
  194. var blocks:Array = new Array();
  195. var len:int = data.length * 8;
  196. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  197. for( var i:int = 0; i < len; i += 8 )
  198. {
  199. blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );
  200. }
  201. // append padding and length
  202. blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
  203. blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
  204. data.position = oldPosition;
  205. return blocks;
  206. }
  207. /**
  208. * Converts a string to a sequence of 16-word blocks
  209. * that we'll do the processing on. Appends padding
  210. * and length in the process.
  211. *
  212. * @param s The string to split into blocks
  213. * @return An array containing the blocks that s was split into.
  214. */
  215. private static function createBlocksFromString( s:String ):Array
  216. {
  217. var blocks:Array = new Array();
  218. var len:int = s.length * 8;
  219. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  220. for( var i:int = 0; i < len; i += 8 ) {
  221. blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 );
  222. }
  223. // append padding and length
  224. blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
  225. blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
  226. return blocks;
  227. }
  228. }
  229. }