index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. (function () {
  2. "use strict";
  3. /**
  4. * md5.js
  5. * Copyright (c) 2011, Yoshinori Kohyama (http://algobit.jp/)
  6. * all rights reserved.
  7. */
  8. exports.digest = function (M) {
  9. var originalLength
  10. , i
  11. , j
  12. , k
  13. , l
  14. , A
  15. , B
  16. , C
  17. , D
  18. , AA
  19. , BB
  20. , CC
  21. , DD
  22. , X
  23. , rval
  24. ;
  25. function F(x, y, z) { return (x & y) | (~x & z); }
  26. function G(x, y, z) { return (x & z) | (y & ~z); }
  27. function H(x, y, z) { return x ^ y ^ z; }
  28. function I(x, y, z) { return y ^ (x | ~z); }
  29. function to4bytes(n) {
  30. return [n&0xff, (n>>>8)&0xff, (n>>>16)&0xff, (n>>>24)&0xff];
  31. }
  32. originalLength = M.length; // for Step.2
  33. // 3.1 Step 1. Append Padding Bits
  34. M.push(0x80);
  35. l = (56 - M.length)&0x3f;
  36. for (i = 0; i < l; i++)
  37. M.push(0);
  38. // 3.2 Step 2. Append Length
  39. to4bytes(8*originalLength).forEach(function (e) { M.push(e); });
  40. [0, 0, 0, 0].forEach(function (e) { M.push(e); });
  41. // 3.3 Step 3. Initialize MD Buffer
  42. A = [0x67452301];
  43. B = [0xefcdab89];
  44. C = [0x98badcfe];
  45. D = [0x10325476];
  46. // 3.4 Step 4. Process Message in 16-Word Blocks
  47. function rounds(a, b, c, d, k, s, t, f) {
  48. a[0] += f(b[0], c[0], d[0]) + X[k] + t;
  49. a[0] = ((a[0]<<s)|(a[0]>>>(32 - s)));
  50. a[0] += b[0];
  51. }
  52. for (i = 0; i < M.length; i += 64) {
  53. X = [];
  54. for (j = 0; j < 64; j += 4) {
  55. k = i + j;
  56. X.push(M[k]|(M[k + 1]<<8)|(M[k + 2]<<16)|(M[k + 3]<<24));
  57. }
  58. AA = A[0];
  59. BB = B[0];
  60. CC = C[0];
  61. DD = D[0];
  62. // Round 1.
  63. rounds(A, B, C, D, 0, 7, 0xd76aa478, F);
  64. rounds(D, A, B, C, 1, 12, 0xe8c7b756, F);
  65. rounds(C, D, A, B, 2, 17, 0x242070db, F);
  66. rounds(B, C, D, A, 3, 22, 0xc1bdceee, F);
  67. rounds(A, B, C, D, 4, 7, 0xf57c0faf, F);
  68. rounds(D, A, B, C, 5, 12, 0x4787c62a, F);
  69. rounds(C, D, A, B, 6, 17, 0xa8304613, F);
  70. rounds(B, C, D, A, 7, 22, 0xfd469501, F);
  71. rounds(A, B, C, D, 8, 7, 0x698098d8, F);
  72. rounds(D, A, B, C, 9, 12, 0x8b44f7af, F);
  73. rounds(C, D, A, B, 10, 17, 0xffff5bb1, F);
  74. rounds(B, C, D, A, 11, 22, 0x895cd7be, F);
  75. rounds(A, B, C, D, 12, 7, 0x6b901122, F);
  76. rounds(D, A, B, C, 13, 12, 0xfd987193, F);
  77. rounds(C, D, A, B, 14, 17, 0xa679438e, F);
  78. rounds(B, C, D, A, 15, 22, 0x49b40821, F);
  79. // Round 2.
  80. rounds(A, B, C, D, 1, 5, 0xf61e2562, G);
  81. rounds(D, A, B, C, 6, 9, 0xc040b340, G);
  82. rounds(C, D, A, B, 11, 14, 0x265e5a51, G);
  83. rounds(B, C, D, A, 0, 20, 0xe9b6c7aa, G);
  84. rounds(A, B, C, D, 5, 5, 0xd62f105d, G);
  85. rounds(D, A, B, C, 10, 9, 0x02441453, G);
  86. rounds(C, D, A, B, 15, 14, 0xd8a1e681, G);
  87. rounds(B, C, D, A, 4, 20, 0xe7d3fbc8, G);
  88. rounds(A, B, C, D, 9, 5, 0x21e1cde6, G);
  89. rounds(D, A, B, C, 14, 9, 0xc33707d6, G);
  90. rounds(C, D, A, B, 3, 14, 0xf4d50d87, G);
  91. rounds(B, C, D, A, 8, 20, 0x455a14ed, G);
  92. rounds(A, B, C, D, 13, 5, 0xa9e3e905, G);
  93. rounds(D, A, B, C, 2, 9, 0xfcefa3f8, G);
  94. rounds(C, D, A, B, 7, 14, 0x676f02d9, G);
  95. rounds(B, C, D, A, 12, 20, 0x8d2a4c8a, G);
  96. // Round 3.
  97. rounds(A, B, C, D, 5, 4, 0xfffa3942, H);
  98. rounds(D, A, B, C, 8, 11, 0x8771f681, H);
  99. rounds(C, D, A, B, 11, 16, 0x6d9d6122, H);
  100. rounds(B, C, D, A, 14, 23, 0xfde5380c, H);
  101. rounds(A, B, C, D, 1, 4, 0xa4beea44, H);
  102. rounds(D, A, B, C, 4, 11, 0x4bdecfa9, H);
  103. rounds(C, D, A, B, 7, 16, 0xf6bb4b60, H);
  104. rounds(B, C, D, A, 10, 23, 0xbebfbc70, H);
  105. rounds(A, B, C, D, 13, 4, 0x289b7ec6, H);
  106. rounds(D, A, B, C, 0, 11, 0xeaa127fa, H);
  107. rounds(C, D, A, B, 3, 16, 0xd4ef3085, H);
  108. rounds(B, C, D, A, 6, 23, 0x04881d05, H);
  109. rounds(A, B, C, D, 9, 4, 0xd9d4d039, H);
  110. rounds(D, A, B, C, 12, 11, 0xe6db99e5, H);
  111. rounds(C, D, A, B, 15, 16, 0x1fa27cf8, H);
  112. rounds(B, C, D, A, 2, 23, 0xc4ac5665, H);
  113. // Round 4.
  114. rounds(A, B, C, D, 0, 6, 0xf4292244, I);
  115. rounds(D, A, B, C, 7, 10, 0x432aff97, I);
  116. rounds(C, D, A, B, 14, 15, 0xab9423a7, I);
  117. rounds(B, C, D, A, 5, 21, 0xfc93a039, I);
  118. rounds(A, B, C, D, 12, 6, 0x655b59c3, I);
  119. rounds(D, A, B, C, 3, 10, 0x8f0ccc92, I);
  120. rounds(C, D, A, B, 10, 15, 0xffeff47d, I);
  121. rounds(B, C, D, A, 1, 21, 0x85845dd1, I);
  122. rounds(A, B, C, D, 8, 6, 0x6fa87e4f, I);
  123. rounds(D, A, B, C, 15, 10, 0xfe2ce6e0, I);
  124. rounds(C, D, A, B, 6, 15, 0xa3014314, I);
  125. rounds(B, C, D, A, 13, 21, 0x4e0811a1, I);
  126. rounds(A, B, C, D, 4, 6, 0xf7537e82, I);
  127. rounds(D, A, B, C, 11, 10, 0xbd3af235, I);
  128. rounds(C, D, A, B, 2, 15, 0x2ad7d2bb, I);
  129. rounds(B, C, D, A, 9, 21, 0xeb86d391, I);
  130. A[0] += AA;
  131. B[0] += BB;
  132. C[0] += CC;
  133. D[0] += DD;
  134. }
  135. rval = [];
  136. to4bytes(A[0]).forEach(function (e) { rval.push(e); });
  137. to4bytes(B[0]).forEach(function (e) { rval.push(e); });
  138. to4bytes(C[0]).forEach(function (e) { rval.push(e); });
  139. to4bytes(D[0]).forEach(function (e) { rval.push(e); });
  140. return rval;
  141. }
  142. exports.digest_s = function (s) {
  143. var M = []
  144. , i
  145. , d
  146. , rstr
  147. , s
  148. ;
  149. for (i = 0; i < s.length; i++)
  150. M.push(s.charCodeAt(i));
  151. d = exports.digest(M);
  152. rstr = '';
  153. d.forEach(function (e) {
  154. s = e.toString(16);
  155. while (s.length < 2)
  156. s = '0' + s;
  157. rstr += s;
  158. });
  159. return rstr;
  160. }
  161. }());