MD5.JS 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * md5.js 1.0b 27/06/96
  3. *
  4. * Javascript implementation of the RSA Data Security, Inc. MD5
  5. * Message-Digest Algorithm.
  6. *
  7. * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
  8. *
  9. * Permission to use, copy, modify, and distribute this software
  10. * and its documentation for any purposes and without
  11. * fee is hereby granted provided that this copyright notice
  12. * appears in all copies.
  13. *
  14. * Of course, this soft is provided "as is" without express or implied
  15. * warranty of any kind.
  16. *
  17. *
  18. * Modified with german comments and some information about collisions.
  19. * (Ralf Mieke, ralf@miekenet.de, http://mieke.home.pages.de)
  20. */
  21. function array(n) {
  22. for(i=0;i<n;i++) this[i]=0;
  23. this.length=n;
  24. }
  25. /* Einige grundlegenden Funktionen müssen wegen
  26. * Javascript Fehlern umgeschrieben werden.
  27. * Man versuche z.B. 0xffffffff >> 4 zu berechnen..
  28. * Die nun verwendeten Funktionen sind zwar langsamer als die Originale,
  29. * aber sie funktionieren.
  30. */
  31. function integer(n) { return n%(0xffffffff+1); }
  32. function shr(a,b) {
  33. a=integer(a);
  34. b=integer(b);
  35. if (a-0x80000000>=0) {
  36. a=a%0x80000000;
  37. a>>=b;
  38. a+=0x40000000>>(b-1);
  39. } else
  40. a>>=b;
  41. return a;
  42. }
  43. function shl1(a) {
  44. a=a%0x80000000;
  45. if (a&0x40000000==0x40000000)
  46. {
  47. a-=0x40000000;
  48. a*=2;
  49. a+=0x80000000;
  50. } else
  51. a*=2;
  52. return a;
  53. }
  54. function shl(a,b) {
  55. a=integer(a);
  56. b=integer(b);
  57. for (var i=0;i<b;i++) a=shl1(a);
  58. return a;
  59. }
  60. function and(a,b) {
  61. a=integer(a);
  62. b=integer(b);
  63. var t1=(a-0x80000000);
  64. var t2=(b-0x80000000);
  65. if (t1>=0)
  66. if (t2>=0)
  67. return ((t1&t2)+0x80000000);
  68. else
  69. return (t1&b);
  70. else
  71. if (t2>=0)
  72. return (a&t2);
  73. else
  74. return (a&b);
  75. }
  76. function or(a,b) {
  77. a=integer(a);
  78. b=integer(b);
  79. var t1=(a-0x80000000);
  80. var t2=(b-0x80000000);
  81. if (t1>=0)
  82. if (t2>=0)
  83. return ((t1|t2)+0x80000000);
  84. else
  85. return ((t1|b)+0x80000000);
  86. else
  87. if (t2>=0)
  88. return ((a|t2)+0x80000000);
  89. else
  90. return (a|b);
  91. }
  92. function xor(a,b) {
  93. a=integer(a);
  94. b=integer(b);
  95. var t1=(a-0x80000000);
  96. var t2=(b-0x80000000);
  97. if (t1>=0)
  98. if (t2>=0)
  99. return (t1^t2);
  100. else
  101. return ((t1^b)+0x80000000);
  102. else
  103. if (t2>=0)
  104. return ((a^t2)+0x80000000);
  105. else
  106. return (a^b);
  107. }
  108. function not(a) {
  109. a=integer(a);
  110. return (0xffffffff-a);
  111. }
  112. /* Beginn des Algorithmus */
  113. var state = new array(4);
  114. var count = new array(2);
  115. count[0] = 0;
  116. count[1] = 0;
  117. var buffer = new array(64);
  118. var transformBuffer = new array(16);
  119. var digestBits = new array(16);
  120. var S11 = 7;
  121. var S12 = 12;
  122. var S13 = 17;
  123. var S14 = 22;
  124. var S21 = 5;
  125. var S22 = 9;
  126. var S23 = 14;
  127. var S24 = 20;
  128. var S31 = 4;
  129. var S32 = 11;
  130. var S33 = 16;
  131. var S34 = 23;
  132. var S41 = 6;
  133. var S42 = 10;
  134. var S43 = 15;
  135. var S44 = 21;
  136. function F(x,y,z) {
  137. return or(and(x,y),and(not(x),z));
  138. }
  139. function G(x,y,z) {
  140. return or(and(x,z),and(y,not(z)));
  141. }
  142. function H(x,y,z) {
  143. return xor(xor(x,y),z);
  144. }
  145. function I(x,y,z) {
  146. return xor(y ,or(x , not(z)));
  147. }
  148. function rotateLeft(a,n) {
  149. return or(shl(a, n),(shr(a,(32 - n))));
  150. }
  151. function FF(a,b,c,d,x,s,ac) {
  152. a = a+F(b, c, d) + x + ac;
  153. a = rotateLeft(a, s);
  154. a = a+b;
  155. return a;
  156. }
  157. function GG(a,b,c,d,x,s,ac) {
  158. a = a+G(b, c, d) +x + ac;
  159. a = rotateLeft(a, s);
  160. a = a+b;
  161. return a;
  162. }
  163. function HH(a,b,c,d,x,s,ac) {
  164. a = a+H(b, c, d) + x + ac;
  165. a = rotateLeft(a, s);
  166. a = a+b;
  167. return a;
  168. }
  169. function II(a,b,c,d,x,s,ac) {
  170. a = a+I(b, c, d) + x + ac;
  171. a = rotateLeft(a, s);
  172. a = a+b;
  173. return a;
  174. }
  175. function transform(buf,offset) {
  176. var a=0, b=0, c=0, d=0;
  177. var x = transformBuffer;
  178. a = state[0];
  179. b = state[1];
  180. c = state[2];
  181. d = state[3];
  182. for (i = 0; i < 16; i++) {
  183. x[i] = and(buf[i*4+offset],0xff);
  184. for (j = 1; j < 4; j++) {
  185. x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
  186. }
  187. }
  188. /* Runde 1 */
  189. a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  190. d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  191. c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  192. b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  193. a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  194. d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  195. c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  196. b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  197. a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  198. d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  199. c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  200. b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  201. a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  202. d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  203. c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  204. b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  205. /* Runde 2 */
  206. a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  207. d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  208. c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  209. b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  210. a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  211. d = GG ( d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  212. c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  213. b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  214. a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  215. d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  216. c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  217. b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  218. a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  219. d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  220. c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  221. b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  222. /* Runde 3 */
  223. a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  224. d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  225. c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  226. b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  227. a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  228. d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  229. c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  230. b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  231. a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  232. d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  233. c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  234. b = HH ( b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  235. a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  236. d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  237. c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  238. b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  239. /* Runde 4 */
  240. a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  241. d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  242. c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  243. b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  244. a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  245. d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  246. c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  247. b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  248. a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  249. d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  250. c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  251. b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  252. a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  253. d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  254. c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  255. b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  256. state[0] +=a;
  257. state[1] +=b;
  258. state[2] +=c;
  259. state[3] +=d;
  260. }
  261. /* Mit der Initialisierung von Dobbertin:
  262. state[0] = 0x12ac2375;
  263. state[1] = 0x3b341042;
  264. state[2] = 0x5f62b97c;
  265. state[3] = 0x4ba763ed;
  266. gibt es eine Kollision:
  267. begin 644 Message1
  268. M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
  269. 39GIK9>TF$W()/MEHR%C4:G1R:Q"=
  270. `
  271. end
  272. begin 644 Message2
  273. M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
  274. 39GIK9>TF$W()/MEHREC4:G1R:Q"=
  275. `
  276. end
  277. */
  278. function init() {
  279. count[0]=count[1] = 0;
  280. state[0] = 0x67452301;
  281. state[1] = 0xefcdab89;
  282. state[2] = 0x98badcfe;
  283. state[3] = 0x10325476;
  284. for (i = 0; i < digestBits.length; i++)
  285. digestBits[i] = 0;
  286. }
  287. function update(b) {
  288. var index,i;
  289. index = and(shr(count[0],3) , 0x3f);
  290. if (count[0]<0xffffffff-7)
  291. count[0] += 8;
  292. else {
  293. count[1]++;
  294. count[0]-=0xffffffff+1;
  295. count[0]+=8;
  296. }
  297. buffer[index] = and(b,0xff);
  298. if (index >= 63) {
  299. transform(buffer, 0);
  300. }
  301. }
  302. function finish() {
  303. var bits = new array(8);
  304. var padding;
  305. var i=0, index=0, padLen=0;
  306. for (i = 0; i < 4; i++) {
  307. bits[i] = and(shr(count[0],(i * 8)), 0xff);
  308. }
  309. for (i = 0; i < 4; i++) {
  310. bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
  311. }
  312. index = and(shr(count[0], 3) ,0x3f);
  313. padLen = (index < 56) ? (56 - index) : (120 - index);
  314. padding = new array(64);
  315. padding[0] = 0x80;
  316. for (i=0;i<padLen;i++)
  317. update(padding[i]);
  318. for (i=0;i<8;i++)
  319. update(bits[i]);
  320. for (i = 0; i < 4; i++) {
  321. for (j = 0; j < 4; j++) {
  322. digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
  323. }
  324. }
  325. }
  326. /* Ende des MD5 Algorithmus */
  327. function hexa(n) {
  328. var hexa_h = "0123456789abcdef";
  329. var hexa_c="";
  330. var hexa_m=n;
  331. for (hexa_i=0;hexa_i<8;hexa_i++) {
  332. hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
  333. hexa_m=Math.floor(hexa_m/16);
  334. }
  335. return hexa_c;
  336. }
  337. var ascii="01234567890123456789012345678901" +
  338. " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
  339. "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
  340. function MD5(nachricht)
  341. {
  342. var l,s,k,ka,kb,kc,kd;
  343. init();
  344. for (k=0;k<nachricht.length;k++) {
  345. l=nachricht.charAt(k);
  346. update(ascii.lastIndexOf(l));
  347. }
  348. finish();
  349. ka=kb=kc=kd=0;
  350. for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
  351. for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
  352. for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
  353. for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
  354. s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
  355. return s;
  356. }