2
0

MD5.as 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. import com.adobe.crypto.IntUtil;
  29. import flash.utils.ByteArray;
  30. /**
  31. * The MD5 Message-Digest Algorithm
  32. *
  33. * Implementation based on algorithm description at
  34. * http://www.faqs.org/rfcs/rfc1321.html
  35. */
  36. public class MD5 {
  37. public static var digest:ByteArray;
  38. /**
  39. * Performs the MD5 hash algorithm on a string.
  40. *
  41. * @param s The string to hash
  42. * @return A string containing the hash value of s
  43. * @langversion ActionScript 3.0
  44. * @playerversion Flash 8.5
  45. * @tiptext
  46. */
  47. public static function hash(s:String) :String{
  48. //Convert to byteArray and send through hashBinary function
  49. // so as to only have complex code in one location
  50. var ba:ByteArray = new ByteArray();
  51. ba.writeUTFBytes(s);
  52. return hashBinary(ba);
  53. }
  54. public static function hashBytes(s:ByteArray) :String{
  55. return hashBinary(s);
  56. }
  57. /**
  58. * Performs the MD5 hash algorithm on a ByteArray.
  59. *
  60. * @param s The string to hash
  61. * @return A string containing the hash value of s
  62. * @langversion ActionScript 3.0
  63. * @playerversion Flash 8.5
  64. * @tiptext
  65. */
  66. public static function hashBinary( s:ByteArray ):String {
  67. // initialize the md buffers
  68. var a:int = 1732584193;
  69. var b:int = -271733879;
  70. var c:int = -1732584194;
  71. var d:int = 271733878;
  72. // variables to store previous values
  73. var aa:int;
  74. var bb:int;
  75. var cc:int;
  76. var dd:int;
  77. // create the blocks from the string and
  78. // save the length as a local var to reduce
  79. // lookup in the loop below
  80. var x:Array = createBlocks( s );
  81. var len:int = x.length;
  82. // loop over all of the blocks
  83. for ( var i:int = 0; i < len; i += 16) {
  84. // save previous values
  85. aa = a;
  86. bb = b;
  87. cc = c;
  88. dd = d;
  89. // Round 1
  90. a = ff( a, b, c, d, x[int(i+ 0)], 7, -680876936 ); // 1
  91. d = ff( d, a, b, c, x[int(i+ 1)], 12, -389564586 ); // 2
  92. c = ff( c, d, a, b, x[int(i+ 2)], 17, 606105819 ); // 3
  93. b = ff( b, c, d, a, x[int(i+ 3)], 22, -1044525330 ); // 4
  94. a = ff( a, b, c, d, x[int(i+ 4)], 7, -176418897 ); // 5
  95. d = ff( d, a, b, c, x[int(i+ 5)], 12, 1200080426 ); // 6
  96. c = ff( c, d, a, b, x[int(i+ 6)], 17, -1473231341 ); // 7
  97. b = ff( b, c, d, a, x[int(i+ 7)], 22, -45705983 ); // 8
  98. a = ff( a, b, c, d, x[int(i+ 8)], 7, 1770035416 ); // 9
  99. d = ff( d, a, b, c, x[int(i+ 9)], 12, -1958414417 ); // 10
  100. c = ff( c, d, a, b, x[int(i+10)], 17, -42063 ); // 11
  101. b = ff( b, c, d, a, x[int(i+11)], 22, -1990404162 ); // 12
  102. a = ff( a, b, c, d, x[int(i+12)], 7, 1804603682 ); // 13
  103. d = ff( d, a, b, c, x[int(i+13)], 12, -40341101 ); // 14
  104. c = ff( c, d, a, b, x[int(i+14)], 17, -1502002290 ); // 15
  105. b = ff( b, c, d, a, x[int(i+15)], 22, 1236535329 ); // 16
  106. // Round 2
  107. a = gg( a, b, c, d, x[int(i+ 1)], 5, -165796510 ); // 17
  108. d = gg( d, a, b, c, x[int(i+ 6)], 9, -1069501632 ); // 18
  109. c = gg( c, d, a, b, x[int(i+11)], 14, 643717713 ); // 19
  110. b = gg( b, c, d, a, x[int(i+ 0)], 20, -373897302 ); // 20
  111. a = gg( a, b, c, d, x[int(i+ 5)], 5, -701558691 ); // 21
  112. d = gg( d, a, b, c, x[int(i+10)], 9, 38016083 ); // 22
  113. c = gg( c, d, a, b, x[int(i+15)], 14, -660478335 ); // 23
  114. b = gg( b, c, d, a, x[int(i+ 4)], 20, -405537848 ); // 24
  115. a = gg( a, b, c, d, x[int(i+ 9)], 5, 568446438 ); // 25
  116. d = gg( d, a, b, c, x[int(i+14)], 9, -1019803690 ); // 26
  117. c = gg( c, d, a, b, x[int(i+ 3)], 14, -187363961 ); // 27
  118. b = gg( b, c, d, a, x[int(i+ 8)], 20, 1163531501 ); // 28
  119. a = gg( a, b, c, d, x[int(i+13)], 5, -1444681467 ); // 29
  120. d = gg( d, a, b, c, x[int(i+ 2)], 9, -51403784 ); // 30
  121. c = gg( c, d, a, b, x[int(i+ 7)], 14, 1735328473 ); // 31
  122. b = gg( b, c, d, a, x[int(i+12)], 20, -1926607734 ); // 32
  123. // Round 3
  124. a = hh( a, b, c, d, x[int(i+ 5)], 4, -378558 ); // 33
  125. d = hh( d, a, b, c, x[int(i+ 8)], 11, -2022574463 ); // 34
  126. c = hh( c, d, a, b, x[int(i+11)], 16, 1839030562 ); // 35
  127. b = hh( b, c, d, a, x[int(i+14)], 23, -35309556 ); // 36
  128. a = hh( a, b, c, d, x[int(i+ 1)], 4, -1530992060 ); // 37
  129. d = hh( d, a, b, c, x[int(i+ 4)], 11, 1272893353 ); // 38
  130. c = hh( c, d, a, b, x[int(i+ 7)], 16, -155497632 ); // 39
  131. b = hh( b, c, d, a, x[int(i+10)], 23, -1094730640 ); // 40
  132. a = hh( a, b, c, d, x[int(i+13)], 4, 681279174 ); // 41
  133. d = hh( d, a, b, c, x[int(i+ 0)], 11, -358537222 ); // 42
  134. c = hh( c, d, a, b, x[int(i+ 3)], 16, -722521979 ); // 43
  135. b = hh( b, c, d, a, x[int(i+ 6)], 23, 76029189 ); // 44
  136. a = hh( a, b, c, d, x[int(i+ 9)], 4, -640364487 ); // 45
  137. d = hh( d, a, b, c, x[int(i+12)], 11, -421815835 ); // 46
  138. c = hh( c, d, a, b, x[int(i+15)], 16, 530742520 ); // 47
  139. b = hh( b, c, d, a, x[int(i+ 2)], 23, -995338651 ); // 48
  140. // Round 4
  141. a = ii( a, b, c, d, x[int(i+ 0)], 6, -198630844 ); // 49
  142. d = ii( d, a, b, c, x[int(i+ 7)], 10, 1126891415 ); // 50
  143. c = ii( c, d, a, b, x[int(i+14)], 15, -1416354905 ); // 51
  144. b = ii( b, c, d, a, x[int(i+ 5)], 21, -57434055 ); // 52
  145. a = ii( a, b, c, d, x[int(i+12)], 6, 1700485571 ); // 53
  146. d = ii( d, a, b, c, x[int(i+ 3)], 10, -1894986606 ); // 54
  147. c = ii( c, d, a, b, x[int(i+10)], 15, -1051523 ); // 55
  148. b = ii( b, c, d, a, x[int(i+ 1)], 21, -2054922799 ); // 56
  149. a = ii( a, b, c, d, x[int(i+ 8)], 6, 1873313359 ); // 57
  150. d = ii( d, a, b, c, x[int(i+15)], 10, -30611744 ); // 58
  151. c = ii( c, d, a, b, x[int(i+ 6)], 15, -1560198380 ); // 59
  152. b = ii( b, c, d, a, x[int(i+13)], 21, 1309151649 ); // 60
  153. a = ii( a, b, c, d, x[int(i+ 4)], 6, -145523070 ); // 61
  154. d = ii( d, a, b, c, x[int(i+11)], 10, -1120210379 ); // 62
  155. c = ii( c, d, a, b, x[int(i+ 2)], 15, 718787259 ); // 63
  156. b = ii( b, c, d, a, x[int(i+ 9)], 21, -343485551 ); // 64
  157. a += aa;
  158. b += bb;
  159. c += cc;
  160. d += dd;
  161. }
  162. digest = new ByteArray()
  163. digest.writeInt(a);
  164. digest.writeInt(b);
  165. digest.writeInt(c);
  166. digest.writeInt(d);
  167. digest.position = 0;
  168. // Finish up by concatening the buffers with their hex output
  169. return IntUtil.toHex( a ) + IntUtil.toHex( b ) + IntUtil.toHex( c ) + IntUtil.toHex( d );
  170. }
  171. /**
  172. * Auxiliary function f as defined in RFC
  173. */
  174. private static function f( x:int, y:int, z:int ):int {
  175. return ( x & y ) | ( (~x) & z );
  176. }
  177. /**
  178. * Auxiliary function g as defined in RFC
  179. */
  180. private static function g( x:int, y:int, z:int ):int {
  181. return ( x & z ) | ( y & (~z) );
  182. }
  183. /**
  184. * Auxiliary function h as defined in RFC
  185. */
  186. private static function h( x:int, y:int, z:int ):int {
  187. return x ^ y ^ z;
  188. }
  189. /**
  190. * Auxiliary function i as defined in RFC
  191. */
  192. private static function i( x:int, y:int, z:int ):int {
  193. return y ^ ( x | (~z) );
  194. }
  195. /**
  196. * A generic transformation function. The logic of ff, gg, hh, and
  197. * ii are all the same, minus the function used, so pull that logic
  198. * out and simplify the method bodies for the transoformation functions.
  199. */
  200. private static function transform( func:Function, a:int, b:int, c:int, d:int, x:int, s:int, t:int):int {
  201. var tmp:int = a + int( func( b, c, d ) ) + x + t;
  202. return IntUtil.rol( tmp, s ) + b;
  203. }
  204. /**
  205. * ff transformation function
  206. */
  207. private static function ff ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
  208. return transform( f, a, b, c, d, x, s, t );
  209. }
  210. /**
  211. * gg transformation function
  212. */
  213. private static function gg ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
  214. return transform( g, a, b, c, d, x, s, t );
  215. }
  216. /**
  217. * hh transformation function
  218. */
  219. private static function hh ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
  220. return transform( h, a, b, c, d, x, s, t );
  221. }
  222. /**
  223. * ii transformation function
  224. */
  225. private static function ii ( a:int, b:int, c:int, d:int, x:int, s:int, t:int ):int {
  226. return transform( i, a, b, c, d, x, s, t );
  227. }
  228. /**
  229. * Converts a string to a sequence of 16-word blocks
  230. * that we'll do the processing on. Appends padding
  231. * and length in the process.
  232. *
  233. * @param s The string to split into blocks
  234. * @return An array containing the blocks that s was
  235. * split into.
  236. */
  237. private static function createBlocks( s:ByteArray ):Array {
  238. var blocks:Array = new Array();
  239. var len:int = s.length * 8;
  240. var mask:int = 0xFF; // ignore hi byte of characters > 0xFF
  241. for( var i:int = 0; i < len; i += 8 ) {
  242. blocks[ int(i >> 5) ] |= ( s[ i / 8 ] & mask ) << ( i % 32 );
  243. }
  244. // append padding and length
  245. blocks[ int(len >> 5) ] |= 0x80 << ( len % 32 );
  246. blocks[ int(( ( ( len + 64 ) >>> 9 ) << 4 ) + 14) ] = len;
  247. return blocks;
  248. }
  249. }
  250. }