PageRenderTime 227ms CodeModel.GetById 21ms RepoModel.GetById 4ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/gsolo/encryption/MD5.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 375 lines | 262 code | 39 blank | 74 comment | 30 complexity | b4c81fb8d38e2919158047c064f23dbb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. package com.gsolo.encryption {
  2. public class MD5 {
  3. /*
  4. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  5. * Digest Algorithm, as defined in RFC 1321.
  6. * Version 2.2-alpha Copyright (C) Paul Johnston 1999 - 2005
  7. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  8. * Distributed under the BSD License
  9. * See http://pajhome.org.uk/crypt/md5 for more info.
  10. *
  11. * Converted to AS3 By Geoffrey Williams
  12. */
  13. /*
  14. * Configurable variables. You may need to tweak these to be compatible with
  15. * the server-side, but the defaults work in most cases.
  16. */
  17. public static const HEX_FORMAT_LOWERCASE:uint = 0;
  18. public static const HEX_FORMAT_UPPERCASE:uint = 1;
  19. public static const BASE64_PAD_CHARACTER_DEFAULT_COMPLIANCE:String = "";
  20. public static const BASE64_PAD_CHARACTER_RFC_COMPLIANCE:String = "=";
  21. public static var hexcase:uint = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  22. public static var b64pad:String = ""; /* base-64 pad character. "=" for strict RFC compliance */
  23. public static function encrypt (string:String):String {
  24. return hex_md5 (string);
  25. }
  26. /*
  27. * These are the functions you'll usually want to call
  28. * They take string arguments and return either hex or base-64 encoded strings
  29. */
  30. public static function hex_md5 (string:String):String {
  31. return rstr2hex (rstr_md5 (str2rstr_utf8 (string)));
  32. }
  33. public static function b64_md5 (string:String):String {
  34. return rstr2b64 (rstr_md5 (str2rstr_utf8 (string)));
  35. }
  36. public static function any_md5 (string:String, encoding:String):String {
  37. return rstr2any (rstr_md5 (str2rstr_utf8 (string)), encoding);
  38. }
  39. public static function hex_hmac_md5 (key:String, data:String):String {
  40. return rstr2hex (rstr_hmac_md5 (str2rstr_utf8 (key), str2rstr_utf8 (data)));
  41. }
  42. public static function b64_hmac_md5 (key:String, data:String):String {
  43. return rstr2b64 (rstr_hmac_md5 (str2rstr_utf8 (key), str2rstr_utf8 (data)));
  44. }
  45. public static function any_hmac_md5 (key:String, data:String, encoding:String):String {
  46. return rstr2any(rstr_hmac_md5(str2rstr_utf8(key), str2rstr_utf8(data)), encoding);
  47. }
  48. /*
  49. * Perform a simple self-test to see if the VM is working
  50. */
  51. public static function md5_vm_test ():Boolean {
  52. return hex_md5 ("abc") == "900150983cd24fb0d6963f7d28e17f72";
  53. }
  54. /*
  55. * Calculate the MD5 of a raw string
  56. */
  57. public static function rstr_md5 (string:String):String {
  58. return binl2rstr (binl_md5 (rstr2binl (string), string.length * 8));
  59. }
  60. /*
  61. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  62. */
  63. public static function rstr_hmac_md5 (key:String, data:String):String {
  64. var bkey:Array = rstr2binl (key);
  65. if (bkey.length > 16) bkey = binl_md5 (bkey, key.length * 8);
  66. var ipad:Array = new Array(16), opad:Array = new Array(16);
  67. for(var i:Number = 0; i < 16; i++) {
  68. ipad[i] = bkey[i] ^ 0x36363636;
  69. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  70. }
  71. var hash:Array = binl_md5 (ipad.concat (rstr2binl (data)), 512 + data.length * 8);
  72. return binl2rstr (binl_md5 (opad.concat (hash), 512 + 128));
  73. }
  74. /*
  75. * Convert a raw string to a hex string
  76. */
  77. public static function rstr2hex (input:String):String {
  78. var hex_tab:String = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  79. var output:String = "";
  80. var x:Number;
  81. for(var i:Number = 0; i < input.length; i++) {
  82. x = input.charCodeAt(i);
  83. output += hex_tab.charAt((x >>> 4) & 0x0F)
  84. + hex_tab.charAt( x & 0x0F);
  85. }
  86. return output;
  87. }
  88. /*
  89. * Convert a raw string to a base-64 string
  90. */
  91. public static function rstr2b64 (input:String):String {
  92. var tab:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  93. var output:String = "";
  94. var len:Number = input.length;
  95. for(var i:Number = 0; i < len; i += 3) {
  96. var triplet:Number = (input.charCodeAt(i) << 16)
  97. | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  98. | (i + 2 < len ? input.charCodeAt(i+2) : 0);
  99. for(var j:Number = 0; j < 4; j++) {
  100. if(i * 8 + j * 6 > input.length * 8) output += b64pad;
  101. else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  102. }
  103. }
  104. return output;
  105. }
  106. /*
  107. * Convert a raw string to an arbitrary string encoding
  108. */
  109. public static function rstr2any(input:String, encoding:String):String {
  110. var divisor:Number = encoding.length;
  111. var remainders:Array = [];
  112. var i:Number, q:Number, x:Number, quotient:Array;
  113. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  114. var dividend:Array = new Array(input.length / 2);
  115. for(i = 0; i < dividend.length; i++) {
  116. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  117. }
  118. /*
  119. * Repeatedly perform a long division. The binary array forms the dividend,
  120. * the length of the encoding is the divisor. Once computed, the quotient
  121. * forms the dividend for the next step. We stop when the dividend is zero.
  122. * All remainders are stored for later use.
  123. */
  124. while(dividend.length > 0) {
  125. quotient = [];
  126. x = 0;
  127. for(i = 0; i < dividend.length; i++) {
  128. x = (x << 16) + dividend[i];
  129. q = Math.floor(x / divisor);
  130. x -= q * divisor;
  131. if(quotient.length > 0 || q > 0)
  132. quotient[quotient.length] = q;
  133. }
  134. remainders[remainders.length] = x;
  135. dividend = quotient;
  136. }
  137. /* Convert the remainders to the output string */
  138. var output:String = "";
  139. for(i = remainders.length - 1; i >= 0; i--)
  140. output += encoding.charAt (remainders[i]);
  141. return output;
  142. }
  143. /*
  144. * Encode a string as utf-8.
  145. * For efficiency, this assumes the input is valid utf-16.
  146. */
  147. public static function str2rstr_utf8 (input:String):String {
  148. var output:String = "";
  149. var i:Number = -1;
  150. var x:Number, y:Number;
  151. while(++i < input.length) {
  152. /* Decode utf-16 surrogate pairs */
  153. x = input.charCodeAt(i);
  154. y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
  155. if(0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
  156. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  157. i++;
  158. }
  159. /* Encode output as utf-8 */
  160. if(x <= 0x7F)
  161. output += String.fromCharCode(x);
  162. else if(x <= 0x7FF)
  163. output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  164. 0x80 | ( x & 0x3F));
  165. else if(x <= 0xFFFF)
  166. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  167. 0x80 | ((x >>> 6 ) & 0x3F),
  168. 0x80 | ( x & 0x3F));
  169. else if(x <= 0x1FFFFF)
  170. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  171. 0x80 | ((x >>> 12) & 0x3F),
  172. 0x80 | ((x >>> 6 ) & 0x3F),
  173. 0x80 | ( x & 0x3F));
  174. }
  175. return output;
  176. }
  177. /*
  178. * Encode a string as utf-16
  179. */
  180. public static function str2rstr_utf16le (input:String):String {
  181. var output:String = "";
  182. for(var i:Number = 0; i < input.length; i++)
  183. output += String.fromCharCode( input.charCodeAt(i) & 0xFF,
  184. (input.charCodeAt(i) >>> 8) & 0xFF);
  185. return output;
  186. }
  187. public static function str2rstr_utf16be (input:String):String {
  188. var output:String = "";
  189. for(var i:Number = 0; i < input.length; i++)
  190. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF,
  191. input.charCodeAt(i) & 0xFF);
  192. return output;
  193. }
  194. /*
  195. * Convert a raw string to an array of little-endian words
  196. * Characters >255 have their high-byte silently ignored.
  197. */
  198. public static function rstr2binl (input:String):Array {
  199. var output:Array = new Array(input.length >> 2);
  200. for(var i:Number = 0; i < output.length; i++)
  201. output[i] = 0;
  202. for(i = 0; i < input.length * 8; i += 8)
  203. output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  204. return output;
  205. }
  206. /*
  207. * Convert an array of little-endian words to a string
  208. */
  209. public static function binl2rstr (input:Array):String {
  210. var output:String = "";
  211. for(var i:Number = 0; i < input.length * 32; i += 8)
  212. output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  213. return output;
  214. }
  215. /*
  216. * Calculate the MD5 of an array of little-endian words, and a bit length.
  217. */
  218. public static function binl_md5 (x:Array, len:Number):Array {
  219. /* append padding */
  220. x[len >> 5] |= 0x80 << ((len) % 32);
  221. x[(((len + 64) >>> 9) << 4) + 14] = len;
  222. var a:Number = 1732584193;
  223. var b:Number = -271733879;
  224. var c:Number = -1732584194;
  225. var d:Number = 271733878;
  226. for(var i:Number = 0; i < x.length; i += 16) {
  227. var olda:Number = a;
  228. var oldb:Number = b;
  229. var oldc:Number = c;
  230. var oldd:Number = d;
  231. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  232. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  233. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  234. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  235. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  236. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  237. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  238. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  239. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  240. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  241. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  242. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  243. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  244. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  245. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  246. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  247. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  248. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  249. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  250. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  251. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  252. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  253. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  254. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  255. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  256. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  257. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  258. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  259. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  260. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  261. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  262. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  263. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  264. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  265. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  266. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  267. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  268. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  269. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  270. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  271. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  272. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  273. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  274. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  275. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  276. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  277. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  278. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  279. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  280. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  281. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  282. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  283. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  284. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  285. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  286. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  287. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  288. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  289. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  290. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  291. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  292. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  293. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  294. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  295. a = safe_add(a, olda);
  296. b = safe_add(b, oldb);
  297. c = safe_add(c, oldc);
  298. d = safe_add(d, oldd);
  299. }
  300. return [a, b, c, d];
  301. }
  302. /*
  303. * These functions implement the four basic operations the algorithm uses.
  304. */
  305. public static function md5_cmn (q:Number, a:Number, b:Number, x:Number, s:Number, t:Number):Number {
  306. return safe_add (bit_rol (safe_add (safe_add (a, q), safe_add(x, t)), s), b);
  307. }
  308. public static function md5_ff (a:Number, b:Number, c:Number, d:Number, x:Number, s:Number, t:Number):Number {
  309. return md5_cmn ((b & c) | ((~b) & d), a, b, x, s, t);
  310. }
  311. public static function md5_gg (a:Number, b:Number, c:Number, d:Number, x:Number, s:Number, t:Number):Number {
  312. return md5_cmn ((b & d) | (c & (~d)), a, b, x, s, t);
  313. }
  314. public static function md5_hh (a:Number, b:Number, c:Number, d:Number, x:Number, s:Number, t:Number):Number {
  315. return md5_cmn (b ^ c ^ d, a, b, x, s, t);
  316. }
  317. public static function md5_ii (a:Number, b:Number, c:Number, d:Number, x:Number, s:Number, t:Number):Number {
  318. return md5_cmn (c ^ (b | (~d)), a, b, x, s, t);
  319. }
  320. /*
  321. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  322. * to work around bugs in some JS interpreters.
  323. */
  324. public static function safe_add (x:Number, y:Number):Number {
  325. var lsw:Number = (x & 0xFFFF) + (y & 0xFFFF);
  326. var msw:Number = (x >> 16) + (y >> 16) + (lsw >> 16);
  327. return (msw << 16) | (lsw & 0xFFFF);
  328. }
  329. /*
  330. * Bitwise rotate a 32-bit number to the left.
  331. */
  332. public static function bit_rol (num:Number, cnt:Number):Number {
  333. return (num << cnt) | (num >>> (32 - cnt));
  334. }
  335. }
  336. }