SHA1.as 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * US Secure Hash Algorithm 1 (SHA1)
  34. *
  35. * Implementation based on algorithm description at
  36. * http://www.faqs.org/rfcs/rfc3174.html
  37. */
  38. public class SHA1
  39. {
  40. public static var digest:ByteArray;
  41. /**
  42. * Performs the SHA1 hash algorithm on a string.
  43. *
  44. * @param s The string to hash
  45. * @return A string containing the hash value of s
  46. * @langversion ActionScript 3.0
  47. * @playerversion 9.0
  48. * @tiptext
  49. */
  50. public static function hash( s:String ):String
  51. {
  52. var blocks:Array = createBlocksFromString( s );
  53. var byteArray:ByteArray = hashBlocks( blocks );
  54. return 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 SHA1 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 = SHA1.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. }
  78. /**
  79. * Performs the SHA1 hash algorithm on a string, then does
  80. * Base64 encoding on the result.
  81. *
  82. * @param s The string to hash
  83. * @return The base64 encoded hash value of s
  84. * @langversion ActionScript 3.0
  85. * @playerversion 9.0
  86. * @tiptext
  87. */
  88. public static function hashToBase64( s:String ):String
  89. {
  90. var blocks:Array = SHA1.createBlocksFromString( s );
  91. var byteArray:ByteArray = hashBlocks(blocks);
  92. // ByteArray.toString() returns the contents as a UTF-8 string,
  93. // which we can't use because certain byte sequences might trigger
  94. // a UTF-8 conversion. Instead, we convert the bytes to characters
  95. // one by one.
  96. var charsInByteArray:String = "";
  97. byteArray.position = 0;
  98. for (var j:int = 0; j < byteArray.length; j++)
  99. {
  100. var byte:uint = byteArray.readUnsignedByte();
  101. charsInByteArray += String.fromCharCode(byte);
  102. }
  103. var encoder:Base64Encoder = new Base64Encoder();
  104. encoder.encode(charsInByteArray);
  105. return encoder.flush();
  106. }
  107. private static function hashBlocks( blocks:Array ):ByteArray
  108. {
  109. // initialize the h's
  110. var h0:int = 0x67452301;
  111. var h1:int = 0xefcdab89;
  112. var h2:int = 0x98badcfe;
  113. var h3:int = 0x10325476;
  114. var h4:int = 0xc3d2e1f0;
  115. var len:int = blocks.length;
  116. var w:Array = new Array( 80 );
  117. // loop over all of the blocks
  118. for ( var i:int = 0; i < len; i += 16 ) {
  119. // 6.1.c
  120. var a:int = h0;
  121. var b:int = h1;
  122. var c:int = h2;
  123. var d:int = h3;
  124. var e:int = h4;
  125. // 80 steps to process each block
  126. // TODO: unroll for faster execution, or 4 loops of
  127. // 20 each to avoid the k and f function calls
  128. for ( var t:int = 0; t < 80; t++ ) {
  129. if ( t < 16 ) {
  130. // 6.1.a
  131. w[ t ] = blocks[ i + t ];
  132. } else {
  133. // 6.1.b
  134. w[ t ] = IntUtil.rol( w[ t - 3 ] ^ w[ t - 8 ] ^ w[ t - 14 ] ^ w[ t - 16 ], 1 );
  135. }
  136. // 6.1.d
  137. var temp:int = IntUtil.rol( a, 5 ) + f( t, b, c, d ) + e + int( w[ t ] ) + k( t );
  138. e = d;
  139. d = c;
  140. c = IntUtil.rol( b, 30 );
  141. b = a;
  142. a = temp;
  143. }
  144. // 6.1.e
  145. h0 += a;
  146. h1 += b;
  147. h2 += c;
  148. h3 += d;
  149. h4 += e;
  150. }
  151. var byteArray:ByteArray = new ByteArray();
  152. byteArray.writeInt(h0);
  153. byteArray.writeInt(h1);
  154. byteArray.writeInt(h2);
  155. byteArray.writeInt(h3);
  156. byteArray.writeInt(h4);
  157. byteArray.position = 0;
  158. digest = new ByteArray();
  159. digest.writeBytes(byteArray);
  160. digest.position = 0;
  161. return byteArray;
  162. }
  163. /**
  164. * Performs the logical function based on t
  165. */
  166. private static function f( t:int, b:int, c:int, d:int ):int {
  167. if ( t < 20 ) {
  168. return ( b & c ) | ( ~b & d );
  169. } else if ( t < 40 ) {
  170. return b ^ c ^ d;
  171. } else if ( t < 60 ) {
  172. return ( b & c ) | ( b & d ) | ( c & d );
  173. }
  174. return b ^ c ^ d;
  175. }
  176. /**
  177. * Determines the constant value based on t
  178. */
  179. private static function k( t:int ):int {
  180. if ( t < 20 ) {
  181. return 0x5a827999;
  182. } else if ( t < 40 ) {
  183. return 0x6ed9eba1;
  184. } else if ( t < 60 ) {
  185. return 0x8f1bbcdc;
  186. }
  187. return 0xca62c1d6;
  188. }
  189. /**
  190. * Converts a ByteArray to a sequence of 16-word blocks
  191. * that we'll do the processing on. Appends padding
  192. * and length in the process.
  193. *
  194. * @param data The data to split into blocks
  195. * @return An array containing the blocks into which data was split
  196. */
  197. private static function createBlocksFromByteArray( data:ByteArray ):Array
  198. {
  199. var oldPosition:int = data.position;
  200. data.position = 0;
  201. var blocks:Array = new Array();
  202. var len:int = data.length * 8;
  203. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  204. for( var i:int = 0; i < len; i += 8 )
  205. {
  206. blocks[ i >> 5 ] |= ( data.readByte() & mask ) << ( 24 - i % 32 );
  207. }
  208. // append padding and length
  209. blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
  210. blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
  211. data.position = oldPosition;
  212. return blocks;
  213. }
  214. /**
  215. * Converts a string to a sequence of 16-word blocks
  216. * that we'll do the processing on. Appends padding
  217. * and length in the process.
  218. *
  219. * @param s The string to split into blocks
  220. * @return An array containing the blocks that s was split into.
  221. */
  222. private static function createBlocksFromString( s:String ):Array
  223. {
  224. var blocks:Array = new Array();
  225. var len:int = s.length * 8;
  226. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  227. for( var i:int = 0; i < len; i += 8 ) {
  228. blocks[ i >> 5 ] |= ( s.charCodeAt( i / 8 ) & mask ) << ( 24 - i % 32 );
  229. }
  230. // append padding and length
  231. blocks[ len >> 5 ] |= 0x80 << ( 24 - len % 32 );
  232. blocks[ ( ( ( len + 64 ) >> 9 ) << 4 ) + 15 ] = len;
  233. return blocks;
  234. }
  235. }
  236. }