UNPKG

9.42 kBJavaScriptView Raw
1;(function (root, factory) {
2 if (typeof exports === "object") {
3 // CommonJS
4 module.exports = exports = factory(require("./core"));
5 }
6 else if (typeof define === "function" && define.amd) {
7 // AMD
8 define(["./core"], factory);
9 }
10 else {
11 // Global (browser)
12 factory(root.CryptoJS);
13 }
14}(this, function (CryptoJS) {
15
16 (function (Math) {
17 // Shortcuts
18 var C = CryptoJS;
19 var C_lib = C.lib;
20 var WordArray = C_lib.WordArray;
21 var Hasher = C_lib.Hasher;
22 var C_algo = C.algo;
23
24 // Constants table
25 var T = [];
26
27 // Compute constants
28 (function () {
29 for (var i = 0; i < 64; i++) {
30 T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
31 }
32 }());
33
34 /**
35 * MD5 hash algorithm.
36 */
37 var MD5 = C_algo.MD5 = Hasher.extend({
38 _doReset: function () {
39 this._hash = new WordArray.init([
40 0x67452301, 0xefcdab89,
41 0x98badcfe, 0x10325476
42 ]);
43 },
44
45 _doProcessBlock: function (M, offset) {
46 // Swap endian
47 for (var i = 0; i < 16; i++) {
48 // Shortcuts
49 var offset_i = offset + i;
50 var M_offset_i = M[offset_i];
51
52 M[offset_i] = (
53 (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
54 (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
55 );
56 }
57
58 // Shortcuts
59 var H = this._hash.words;
60
61 var M_offset_0 = M[offset + 0];
62 var M_offset_1 = M[offset + 1];
63 var M_offset_2 = M[offset + 2];
64 var M_offset_3 = M[offset + 3];
65 var M_offset_4 = M[offset + 4];
66 var M_offset_5 = M[offset + 5];
67 var M_offset_6 = M[offset + 6];
68 var M_offset_7 = M[offset + 7];
69 var M_offset_8 = M[offset + 8];
70 var M_offset_9 = M[offset + 9];
71 var M_offset_10 = M[offset + 10];
72 var M_offset_11 = M[offset + 11];
73 var M_offset_12 = M[offset + 12];
74 var M_offset_13 = M[offset + 13];
75 var M_offset_14 = M[offset + 14];
76 var M_offset_15 = M[offset + 15];
77
78 // Working variables
79 var a = H[0];
80 var b = H[1];
81 var c = H[2];
82 var d = H[3];
83
84 // Computation
85 a = FF(a, b, c, d, M_offset_0, 7, T[0]);
86 d = FF(d, a, b, c, M_offset_1, 12, T[1]);
87 c = FF(c, d, a, b, M_offset_2, 17, T[2]);
88 b = FF(b, c, d, a, M_offset_3, 22, T[3]);
89 a = FF(a, b, c, d, M_offset_4, 7, T[4]);
90 d = FF(d, a, b, c, M_offset_5, 12, T[5]);
91 c = FF(c, d, a, b, M_offset_6, 17, T[6]);
92 b = FF(b, c, d, a, M_offset_7, 22, T[7]);
93 a = FF(a, b, c, d, M_offset_8, 7, T[8]);
94 d = FF(d, a, b, c, M_offset_9, 12, T[9]);
95 c = FF(c, d, a, b, M_offset_10, 17, T[10]);
96 b = FF(b, c, d, a, M_offset_11, 22, T[11]);
97 a = FF(a, b, c, d, M_offset_12, 7, T[12]);
98 d = FF(d, a, b, c, M_offset_13, 12, T[13]);
99 c = FF(c, d, a, b, M_offset_14, 17, T[14]);
100 b = FF(b, c, d, a, M_offset_15, 22, T[15]);
101
102 a = GG(a, b, c, d, M_offset_1, 5, T[16]);
103 d = GG(d, a, b, c, M_offset_6, 9, T[17]);
104 c = GG(c, d, a, b, M_offset_11, 14, T[18]);
105 b = GG(b, c, d, a, M_offset_0, 20, T[19]);
106 a = GG(a, b, c, d, M_offset_5, 5, T[20]);
107 d = GG(d, a, b, c, M_offset_10, 9, T[21]);
108 c = GG(c, d, a, b, M_offset_15, 14, T[22]);
109 b = GG(b, c, d, a, M_offset_4, 20, T[23]);
110 a = GG(a, b, c, d, M_offset_9, 5, T[24]);
111 d = GG(d, a, b, c, M_offset_14, 9, T[25]);
112 c = GG(c, d, a, b, M_offset_3, 14, T[26]);
113 b = GG(b, c, d, a, M_offset_8, 20, T[27]);
114 a = GG(a, b, c, d, M_offset_13, 5, T[28]);
115 d = GG(d, a, b, c, M_offset_2, 9, T[29]);
116 c = GG(c, d, a, b, M_offset_7, 14, T[30]);
117 b = GG(b, c, d, a, M_offset_12, 20, T[31]);
118
119 a = HH(a, b, c, d, M_offset_5, 4, T[32]);
120 d = HH(d, a, b, c, M_offset_8, 11, T[33]);
121 c = HH(c, d, a, b, M_offset_11, 16, T[34]);
122 b = HH(b, c, d, a, M_offset_14, 23, T[35]);
123 a = HH(a, b, c, d, M_offset_1, 4, T[36]);
124 d = HH(d, a, b, c, M_offset_4, 11, T[37]);
125 c = HH(c, d, a, b, M_offset_7, 16, T[38]);
126 b = HH(b, c, d, a, M_offset_10, 23, T[39]);
127 a = HH(a, b, c, d, M_offset_13, 4, T[40]);
128 d = HH(d, a, b, c, M_offset_0, 11, T[41]);
129 c = HH(c, d, a, b, M_offset_3, 16, T[42]);
130 b = HH(b, c, d, a, M_offset_6, 23, T[43]);
131 a = HH(a, b, c, d, M_offset_9, 4, T[44]);
132 d = HH(d, a, b, c, M_offset_12, 11, T[45]);
133 c = HH(c, d, a, b, M_offset_15, 16, T[46]);
134 b = HH(b, c, d, a, M_offset_2, 23, T[47]);
135
136 a = II(a, b, c, d, M_offset_0, 6, T[48]);
137 d = II(d, a, b, c, M_offset_7, 10, T[49]);
138 c = II(c, d, a, b, M_offset_14, 15, T[50]);
139 b = II(b, c, d, a, M_offset_5, 21, T[51]);
140 a = II(a, b, c, d, M_offset_12, 6, T[52]);
141 d = II(d, a, b, c, M_offset_3, 10, T[53]);
142 c = II(c, d, a, b, M_offset_10, 15, T[54]);
143 b = II(b, c, d, a, M_offset_1, 21, T[55]);
144 a = II(a, b, c, d, M_offset_8, 6, T[56]);
145 d = II(d, a, b, c, M_offset_15, 10, T[57]);
146 c = II(c, d, a, b, M_offset_6, 15, T[58]);
147 b = II(b, c, d, a, M_offset_13, 21, T[59]);
148 a = II(a, b, c, d, M_offset_4, 6, T[60]);
149 d = II(d, a, b, c, M_offset_11, 10, T[61]);
150 c = II(c, d, a, b, M_offset_2, 15, T[62]);
151 b = II(b, c, d, a, M_offset_9, 21, T[63]);
152
153 // Intermediate hash value
154 H[0] = (H[0] + a) | 0;
155 H[1] = (H[1] + b) | 0;
156 H[2] = (H[2] + c) | 0;
157 H[3] = (H[3] + d) | 0;
158 },
159
160 _doFinalize: function () {
161 // Shortcuts
162 var data = this._data;
163 var dataWords = data.words;
164
165 var nBitsTotal = this._nDataBytes * 8;
166 var nBitsLeft = data.sigBytes * 8;
167
168 // Add padding
169 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
170
171 var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
172 var nBitsTotalL = nBitsTotal;
173 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
174 (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
175 (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
176 );
177 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
178 (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
179 (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
180 );
181
182 data.sigBytes = (dataWords.length + 1) * 4;
183
184 // Hash final blocks
185 this._process();
186
187 // Shortcuts
188 var hash = this._hash;
189 var H = hash.words;
190
191 // Swap endian
192 for (var i = 0; i < 4; i++) {
193 // Shortcut
194 var H_i = H[i];
195
196 H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
197 (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
198 }
199
200 // Return final computed hash
201 return hash;
202 },
203
204 clone: function () {
205 var clone = Hasher.clone.call(this);
206 clone._hash = this._hash.clone();
207
208 return clone;
209 }
210 });
211
212 function FF(a, b, c, d, x, s, t) {
213 var n = a + ((b & c) | (~b & d)) + x + t;
214 return ((n << s) | (n >>> (32 - s))) + b;
215 }
216
217 function GG(a, b, c, d, x, s, t) {
218 var n = a + ((b & d) | (c & ~d)) + x + t;
219 return ((n << s) | (n >>> (32 - s))) + b;
220 }
221
222 function HH(a, b, c, d, x, s, t) {
223 var n = a + (b ^ c ^ d) + x + t;
224 return ((n << s) | (n >>> (32 - s))) + b;
225 }
226
227 function II(a, b, c, d, x, s, t) {
228 var n = a + (c ^ (b | ~d)) + x + t;
229 return ((n << s) | (n >>> (32 - s))) + b;
230 }
231
232 /**
233 * Shortcut function to the hasher's object interface.
234 *
235 * @param {WordArray|string} message The message to hash.
236 *
237 * @return {WordArray} The hash.
238 *
239 * @static
240 *
241 * @example
242 *
243 * var hash = CryptoJS.MD5('message');
244 * var hash = CryptoJS.MD5(wordArray);
245 */
246 C.MD5 = Hasher._createHelper(MD5);
247
248 /**
249 * Shortcut function to the HMAC's object interface.
250 *
251 * @param {WordArray|string} message The message to hash.
252 * @param {WordArray|string} key The secret key.
253 *
254 * @return {WordArray} The HMAC.
255 *
256 * @static
257 *
258 * @example
259 *
260 * var hmac = CryptoJS.HmacMD5(message, key);
261 */
262 C.HmacMD5 = Hasher._createHmacHelper(MD5);
263 }(Math));
264
265
266 return CryptoJS.MD5;
267
268}));
\No newline at end of file