PageRenderTime 35ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/hash/MD5.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 204 lines | 156 code | 20 blank | 28 comment | 4 complexity | c5e7c3a4e53c1145675d5697e6216def MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * MD5
  3. *
  4. * An ActionScript 3 implementation of the RSA Data Security, Inc. MD5 Message
  5. * Digest Algorithm, as defined in RFC 1321.
  6. * Copyright (c) 2007 Henri Torgemane
  7. *
  8. * Derived from
  9. * A JavaScript implementation of the same.
  10. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
  11. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  12. *
  13. * Note:
  14. * This algorithm should not be your first choice for new developements, but is
  15. * included to allow interoperability with existing codes and protocols.
  16. *
  17. * See LICENSE.txt for full license information.
  18. */
  19. package com.hurlant.crypto.hash
  20. {
  21. import flash.utils.ByteArray;
  22. import flash.utils.Endian;
  23. public class MD5 implements IHash
  24. {
  25. public static const HASH_SIZE:int = 16;
  26. public var pad_size:int = 48;
  27. public function MD5() { }
  28. public function getInputSize():uint
  29. {
  30. return 64;
  31. }
  32. public function getHashSize():uint
  33. {
  34. return HASH_SIZE;
  35. }
  36. public function getPadSize():int
  37. {
  38. return pad_size;
  39. }
  40. public function hash(src:ByteArray):ByteArray
  41. {
  42. var len:uint = src.length *8;
  43. var savedEndian:String = src.endian;
  44. // pad to nearest int.
  45. while (src.length%4!=0) {
  46. src[src.length]=0;
  47. }
  48. // convert ByteArray to an array of uint
  49. src.position=0;
  50. var a:Array = [];
  51. src.endian=Endian.LITTLE_ENDIAN
  52. for (var i:uint=0;i<src.length;i+=4) {
  53. a.push(src.readUnsignedInt());
  54. }
  55. var h:Array = core_md5(a, len);
  56. var out:ByteArray = new ByteArray;
  57. out.endian=Endian.LITTLE_ENDIAN;
  58. for (i=0;i<4;i++) {
  59. out.writeUnsignedInt(h[i]);
  60. }
  61. // restore length!
  62. src.length = len/8;
  63. src.endian = savedEndian;
  64. return out;
  65. }
  66. private function core_md5(x:Array, len:uint):Array {
  67. /* append padding */
  68. x[len >> 5] |= 0x80 << ((len) % 32);
  69. x[(((len + 64) >>> 9) << 4) + 14] = len;
  70. var a:uint = 0x67452301; // 1732584193;
  71. var b:uint = 0xEFCDAB89; //-271733879;
  72. var c:uint = 0x98BADCFE; //-1732584194;
  73. var d:uint = 0x10325476; // 271733878;
  74. for(var i:uint = 0; i < x.length; i += 16)
  75. {
  76. x[i]||=0; x[i+1]||=0; x[i+2]||=0; x[i+3]||=0;
  77. x[i+4]||=0; x[i+5]||=0; x[i+6]||=0; x[i+7]||=0;
  78. x[i+8]||=0; x[i+9]||=0; x[i+10]||=0; x[i+11]||=0;
  79. x[i+12]||=0; x[i+13]||=0; x[i+14]||=0; x[i+15]||=0;
  80. var olda:uint = a;
  81. var oldb:uint = b;
  82. var oldc:uint = c;
  83. var oldd:uint = d;
  84. a = ff(a, b, c, d, x[i+ 0], 7 , 0xD76AA478);
  85. d = ff(d, a, b, c, x[i+ 1], 12, 0xE8C7B756);
  86. c = ff(c, d, a, b, x[i+ 2], 17, 0x242070DB);
  87. b = ff(b, c, d, a, x[i+ 3], 22, 0xC1BDCEEE);
  88. a = ff(a, b, c, d, x[i+ 4], 7 , 0xF57C0FAF);
  89. d = ff(d, a, b, c, x[i+ 5], 12, 0x4787C62A);
  90. c = ff(c, d, a, b, x[i+ 6], 17, 0xA8304613);
  91. b = ff(b, c, d, a, x[i+ 7], 22, 0xFD469501);
  92. a = ff(a, b, c, d, x[i+ 8], 7 , 0x698098D8);
  93. d = ff(d, a, b, c, x[i+ 9], 12, 0x8B44F7AF);
  94. c = ff(c, d, a, b, x[i+10], 17, 0xFFFF5BB1);
  95. b = ff(b, c, d, a, x[i+11], 22, 0x895CD7BE);
  96. a = ff(a, b, c, d, x[i+12], 7 , 0x6B901122);
  97. d = ff(d, a, b, c, x[i+13], 12, 0xFD987193);
  98. c = ff(c, d, a, b, x[i+14], 17, 0xA679438E);
  99. b = ff(b, c, d, a, x[i+15], 22, 0x49B40821);
  100. a = gg(a, b, c, d, x[i+ 1], 5 , 0xf61e2562);
  101. d = gg(d, a, b, c, x[i+ 6], 9 , 0xc040b340);
  102. c = gg(c, d, a, b, x[i+11], 14, 0x265e5a51);
  103. b = gg(b, c, d, a, x[i+ 0], 20, 0xe9b6c7aa);
  104. a = gg(a, b, c, d, x[i+ 5], 5 , 0xd62f105d);
  105. d = gg(d, a, b, c, x[i+10], 9 , 0x2441453);
  106. c = gg(c, d, a, b, x[i+15], 14, 0xd8a1e681);
  107. b = gg(b, c, d, a, x[i+ 4], 20, 0xe7d3fbc8);
  108. a = gg(a, b, c, d, x[i+ 9], 5 , 0x21e1cde6);
  109. d = gg(d, a, b, c, x[i+14], 9 , 0xc33707d6);
  110. c = gg(c, d, a, b, x[i+ 3], 14, 0xf4d50d87);
  111. b = gg(b, c, d, a, x[i+ 8], 20, 0x455a14ed);
  112. a = gg(a, b, c, d, x[i+13], 5 , 0xa9e3e905);
  113. d = gg(d, a, b, c, x[i+ 2], 9 , 0xfcefa3f8);
  114. c = gg(c, d, a, b, x[i+ 7], 14, 0x676f02d9);
  115. b = gg(b, c, d, a, x[i+12], 20, 0x8d2a4c8a);
  116. a = hh(a, b, c, d, x[i+ 5], 4 , 0xfffa3942);
  117. d = hh(d, a, b, c, x[i+ 8], 11, 0x8771f681);
  118. c = hh(c, d, a, b, x[i+11], 16, 0x6d9d6122);
  119. b = hh(b, c, d, a, x[i+14], 23, 0xfde5380c);
  120. a = hh(a, b, c, d, x[i+ 1], 4 , 0xa4beea44);
  121. d = hh(d, a, b, c, x[i+ 4], 11, 0x4bdecfa9);
  122. c = hh(c, d, a, b, x[i+ 7], 16, 0xf6bb4b60);
  123. b = hh(b, c, d, a, x[i+10], 23, 0xbebfbc70);
  124. a = hh(a, b, c, d, x[i+13], 4 , 0x289b7ec6);
  125. d = hh(d, a, b, c, x[i+ 0], 11, 0xeaa127fa);
  126. c = hh(c, d, a, b, x[i+ 3], 16, 0xd4ef3085);
  127. b = hh(b, c, d, a, x[i+ 6], 23, 0x4881d05);
  128. a = hh(a, b, c, d, x[i+ 9], 4 , 0xd9d4d039);
  129. d = hh(d, a, b, c, x[i+12], 11, 0xe6db99e5);
  130. c = hh(c, d, a, b, x[i+15], 16, 0x1fa27cf8);
  131. b = hh(b, c, d, a, x[i+ 2], 23, 0xc4ac5665);
  132. a = ii(a, b, c, d, x[i+ 0], 6 , 0xf4292244);
  133. d = ii(d, a, b, c, x[i+ 7], 10, 0x432aff97);
  134. c = ii(c, d, a, b, x[i+14], 15, 0xab9423a7);
  135. b = ii(b, c, d, a, x[i+ 5], 21, 0xfc93a039);
  136. a = ii(a, b, c, d, x[i+12], 6 , 0x655b59c3);
  137. d = ii(d, a, b, c, x[i+ 3], 10, 0x8f0ccc92);
  138. c = ii(c, d, a, b, x[i+10], 15, 0xffeff47d);
  139. b = ii(b, c, d, a, x[i+ 1], 21, 0x85845dd1);
  140. a = ii(a, b, c, d, x[i+ 8], 6 , 0x6fa87e4f);
  141. d = ii(d, a, b, c, x[i+15], 10, 0xfe2ce6e0);
  142. c = ii(c, d, a, b, x[i+ 6], 15, 0xa3014314);
  143. b = ii(b, c, d, a, x[i+13], 21, 0x4e0811a1);
  144. a = ii(a, b, c, d, x[i+ 4], 6 , 0xf7537e82);
  145. d = ii(d, a, b, c, x[i+11], 10, 0xbd3af235);
  146. c = ii(c, d, a, b, x[i+ 2], 15, 0x2ad7d2bb);
  147. b = ii(b, c, d, a, x[i+ 9], 21, 0xeb86d391);
  148. a += olda;
  149. b += oldb;
  150. c += oldc;
  151. d += oldd;
  152. }
  153. return [ a, b, c, d ];
  154. }
  155. /*
  156. * Bitwise rotate a 32-bit number to the left.
  157. */
  158. private function rol(num:uint, cnt:uint):uint
  159. {
  160. return (num << cnt) | (num >>> (32 - cnt));
  161. }
  162. /*
  163. * These functions implement the four basic operations the algorithm uses.
  164. */
  165. private function cmn(q:uint, a:uint, b:uint, x:uint, s:uint, t:uint):uint {
  166. return rol(a + q + x + t, s) + b;
  167. }
  168. private function ff(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
  169. return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  170. }
  171. private function gg(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
  172. return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  173. }
  174. private function hh(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
  175. return cmn(b ^ c ^ d, a, b, x, s, t);
  176. }
  177. private function ii(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
  178. return cmn(c ^ (b | (~d)), a, b, x, s, t);
  179. }
  180. public function toString():String {
  181. return "md5";
  182. }
  183. }
  184. }