MD5.as 16 KB

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