5e23601fb44d35e55590af5860d7186629edd203.svn-base 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. if (!dojo._hasResource["dojox.crypto.MD5"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.crypto.MD5"] = true;
  6. dojo.provide("dojox.crypto.MD5");
  7. dojo.require("dojox.crypto._base");
  8. /*
  9. * Return to a port of Paul Johnstone's MD5 implementation
  10. * http://pajhome.org.uk/crypt/md5/index.html
  11. *
  12. * Copyright (C) Paul Johnston 1999 - 2002. Other contributors: Greg Holt,
  13. * Andrew Kepert, Ydnar, Lostinet Distributed under the BSD License
  14. *
  15. * Dojo port by Tom Trenka
  16. *
  17. * 2005-12-7 All conversions are internalized (no dependencies) implemented
  18. * getHMAC for message digest auth.
  19. */
  20. dojox.crypto.MD5 = new function() {
  21. // summary
  22. // object for creating digests using the MD5 algorithm
  23. var chrsz = 8;
  24. var mask = (1 << chrsz) - 1;
  25. function toWord(s) {
  26. var wa = [];
  27. for (var i = 0; i < s.length * chrsz; i += chrsz)
  28. wa[i >> 5] |= (s.charCodeAt(i / chrsz) & mask) << (i % 32);
  29. return wa;
  30. }
  31. function toString(wa) {
  32. var s = [];
  33. for (var i = 0; i < wa.length * 32; i += chrsz)
  34. s.push(String.fromCharCode((wa[i >> 5] >>> (i % 32)) & mask));
  35. return s.join("");
  36. }
  37. function toHex(wa) {
  38. var h = "0123456789abcdef";
  39. var s = [];
  40. for (var i = 0; i < wa.length * 4; i++) {
  41. s.push(h.charAt((wa[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF)
  42. + h.charAt((wa[i >> 2] >> ((i % 4) * 8)) & 0xF));
  43. }
  44. return s.join("");
  45. }
  46. function toBase64(wa) {
  47. var p = "=";
  48. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  49. var s = [];
  50. for (var i = 0; i < wa.length * 4; i += 3) {
  51. var t = (((wa[i >> 2] >> 8 * (i % 4)) & 0xFF) << 16)
  52. | (((wa[i + 1 >> 2] >> 8 * ((i + 1) % 4)) & 0xFF) << 8)
  53. | ((wa[i + 2 >> 2] >> 8 * ((i + 2) % 4)) & 0xFF);
  54. for (var j = 0; j < 4; j++) {
  55. if (i * 8 + j * 6 > wa.length * 32)
  56. s.push(p);
  57. else
  58. s.push(tab.charAt((t >> 6 * (3 - j)) & 0x3F));
  59. }
  60. }
  61. return s.join("");
  62. }
  63. function add(x, y) {
  64. var l = (x & 0xFFFF) + (y & 0xFFFF);
  65. var m = (x >> 16) + (y >> 16) + (l >> 16);
  66. return (m << 16) | (l & 0xFFFF);
  67. }
  68. function R(n, c) {
  69. return (n << c) | (n >>> (32 - c));
  70. }
  71. function C(q, a, b, x, s, t) {
  72. return add(R(add(add(a, q), add(x, t)), s), b);
  73. }
  74. function FF(a, b, c, d, x, s, t) {
  75. return C((b & c) | ((~b) & d), a, b, x, s, t);
  76. }
  77. function GG(a, b, c, d, x, s, t) {
  78. return C((b & d) | (c & (~d)), a, b, x, s, t);
  79. }
  80. function HH(a, b, c, d, x, s, t) {
  81. return C(b ^ c ^ d, a, b, x, s, t);
  82. }
  83. function II(a, b, c, d, x, s, t) {
  84. return C(c ^ (b | (~d)), a, b, x, s, t);
  85. }
  86. function core(x, len) {
  87. x[len >> 5] |= 0x80 << ((len) % 32);
  88. x[(((len + 64) >>> 9) << 4) + 14] = len;
  89. var a = 1732584193;
  90. var b = -271733879;
  91. var c = -1732584194;
  92. var d = 271733878;
  93. for (var i = 0; i < x.length; i += 16) {
  94. var olda = a;
  95. var oldb = b;
  96. var oldc = c;
  97. var oldd = d;
  98. a = FF(a, b, c, d, x[i + 0], 7, -680876936);
  99. d = FF(d, a, b, c, x[i + 1], 12, -389564586);
  100. c = FF(c, d, a, b, x[i + 2], 17, 606105819);
  101. b = FF(b, c, d, a, x[i + 3], 22, -1044525330);
  102. a = FF(a, b, c, d, x[i + 4], 7, -176418897);
  103. d = FF(d, a, b, c, x[i + 5], 12, 1200080426);
  104. c = FF(c, d, a, b, x[i + 6], 17, -1473231341);
  105. b = FF(b, c, d, a, x[i + 7], 22, -45705983);
  106. a = FF(a, b, c, d, x[i + 8], 7, 1770035416);
  107. d = FF(d, a, b, c, x[i + 9], 12, -1958414417);
  108. c = FF(c, d, a, b, x[i + 10], 17, -42063);
  109. b = FF(b, c, d, a, x[i + 11], 22, -1990404162);
  110. a = FF(a, b, c, d, x[i + 12], 7, 1804603682);
  111. d = FF(d, a, b, c, x[i + 13], 12, -40341101);
  112. c = FF(c, d, a, b, x[i + 14], 17, -1502002290);
  113. b = FF(b, c, d, a, x[i + 15], 22, 1236535329);
  114. a = GG(a, b, c, d, x[i + 1], 5, -165796510);
  115. d = GG(d, a, b, c, x[i + 6], 9, -1069501632);
  116. c = GG(c, d, a, b, x[i + 11], 14, 643717713);
  117. b = GG(b, c, d, a, x[i + 0], 20, -373897302);
  118. a = GG(a, b, c, d, x[i + 5], 5, -701558691);
  119. d = GG(d, a, b, c, x[i + 10], 9, 38016083);
  120. c = GG(c, d, a, b, x[i + 15], 14, -660478335);
  121. b = GG(b, c, d, a, x[i + 4], 20, -405537848);
  122. a = GG(a, b, c, d, x[i + 9], 5, 568446438);
  123. d = GG(d, a, b, c, x[i + 14], 9, -1019803690);
  124. c = GG(c, d, a, b, x[i + 3], 14, -187363961);
  125. b = GG(b, c, d, a, x[i + 8], 20, 1163531501);
  126. a = GG(a, b, c, d, x[i + 13], 5, -1444681467);
  127. d = GG(d, a, b, c, x[i + 2], 9, -51403784);
  128. c = GG(c, d, a, b, x[i + 7], 14, 1735328473);
  129. b = GG(b, c, d, a, x[i + 12], 20, -1926607734);
  130. a = HH(a, b, c, d, x[i + 5], 4, -378558);
  131. d = HH(d, a, b, c, x[i + 8], 11, -2022574463);
  132. c = HH(c, d, a, b, x[i + 11], 16, 1839030562);
  133. b = HH(b, c, d, a, x[i + 14], 23, -35309556);
  134. a = HH(a, b, c, d, x[i + 1], 4, -1530992060);
  135. d = HH(d, a, b, c, x[i + 4], 11, 1272893353);
  136. c = HH(c, d, a, b, x[i + 7], 16, -155497632);
  137. b = HH(b, c, d, a, x[i + 10], 23, -1094730640);
  138. a = HH(a, b, c, d, x[i + 13], 4, 681279174);
  139. d = HH(d, a, b, c, x[i + 0], 11, -358537222);
  140. c = HH(c, d, a, b, x[i + 3], 16, -722521979);
  141. b = HH(b, c, d, a, x[i + 6], 23, 76029189);
  142. a = HH(a, b, c, d, x[i + 9], 4, -640364487);
  143. d = HH(d, a, b, c, x[i + 12], 11, -421815835);
  144. c = HH(c, d, a, b, x[i + 15], 16, 530742520);
  145. b = HH(b, c, d, a, x[i + 2], 23, -995338651);
  146. a = II(a, b, c, d, x[i + 0], 6, -198630844);
  147. d = II(d, a, b, c, x[i + 7], 10, 1126891415);
  148. c = II(c, d, a, b, x[i + 14], 15, -1416354905);
  149. b = II(b, c, d, a, x[i + 5], 21, -57434055);
  150. a = II(a, b, c, d, x[i + 12], 6, 1700485571);
  151. d = II(d, a, b, c, x[i + 3], 10, -1894986606);
  152. c = II(c, d, a, b, x[i + 10], 15, -1051523);
  153. b = II(b, c, d, a, x[i + 1], 21, -2054922799);
  154. a = II(a, b, c, d, x[i + 8], 6, 1873313359);
  155. d = II(d, a, b, c, x[i + 15], 10, -30611744);
  156. c = II(c, d, a, b, x[i + 6], 15, -1560198380);
  157. b = II(b, c, d, a, x[i + 13], 21, 1309151649);
  158. a = II(a, b, c, d, x[i + 4], 6, -145523070);
  159. d = II(d, a, b, c, x[i + 11], 10, -1120210379);
  160. c = II(c, d, a, b, x[i + 2], 15, 718787259);
  161. b = II(b, c, d, a, x[i + 9], 21, -343485551);
  162. a = add(a, olda);
  163. b = add(b, oldb);
  164. c = add(c, oldc);
  165. d = add(d, oldd);
  166. }
  167. return [a, b, c, d];
  168. }
  169. function hmac(data, key) {
  170. var wa = toWord(key);
  171. if (wa.length > 16)
  172. wa = core(wa, key.length * chrsz);
  173. var l = [], r = [];
  174. for (var i = 0; i < 16; i++) {
  175. l[i] = wa[i] ^ 0x36363636;
  176. r[i] = wa[i] ^ 0x5c5c5c5c;
  177. }
  178. var h = core(l.concat(toWord(data)), 512 + data.length * chrsz);
  179. return core(r.concat(h), 640);
  180. }
  181. // Public functions
  182. this.compute = function(/* string */data, /* dojox.crypto.outputTypes */
  183. outputType) {
  184. // summary
  185. // computes the digest of data, and returns the result as a string
  186. // of type outputType
  187. var out = outputType || dojox.crypto.outputTypes.Base64;
  188. switch (out) {
  189. case dojox.crypto.outputTypes.Hex : {
  190. return toHex(core(toWord(data), data.length * chrsz)); // string
  191. }
  192. case dojox.crypto.outputTypes.String : {
  193. return toString(core(toWord(data), data.length * chrsz)); // string
  194. }
  195. default : {
  196. return toBase64(core(toWord(data), data.length * chrsz)); // string
  197. }
  198. }
  199. };
  200. this.getHMAC = function(/* string */data, /* string */key, /* dojox.crypto.outputTypes */
  201. outputType) {
  202. // summary
  203. // computes a digest of data using key, and returns the result as a
  204. // string of outputType
  205. var out = outputType || dojox.crypto.outputTypes.Base64;
  206. switch (out) {
  207. case dojox.crypto.outputTypes.Hex : {
  208. return toHex(hmac(data, key)); // string
  209. }
  210. case dojox.crypto.outputTypes.String : {
  211. return toString(hmac(data, key)); // string
  212. }
  213. default : {
  214. return toBase64(hmac(data, key)); // string
  215. }
  216. }
  217. };
  218. }();
  219. }