comparison md5.js @ 0:96898e3812a5

initial push
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 18 Dec 2011 19:03:28 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:96898e3812a5
1 /*
2 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
3 * Digest Algorithm, as defined in RFC 1321.
4 * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
5 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6 * Distributed under the BSD License
7 * See http://pajhome.org.uk/crypt/md5 for more info.
8 */
9
10 var MD5 = (function () {
11 /*
12 * Configurable variables. You may need to tweak these to be compatible with
13 * the server-side, but the defaults work in most cases.
14 */
15 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
16 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
17 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
18
19 /*
20 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
21 * to work around bugs in some JS interpreters.
22 */
23 var safe_add = function (x, y) {
24 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
25 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
26 return (msw << 16) | (lsw & 0xFFFF);
27 };
28
29 /*
30 * Bitwise rotate a 32-bit number to the left.
31 */
32 var bit_rol = function (num, cnt) {
33 return (num << cnt) | (num >>> (32 - cnt));
34 };
35
36 /*
37 * Convert a string to an array of little-endian words
38 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
39 */
40 var str2binl = function (str) {
41 var bin = [];
42 var mask = (1 << chrsz) - 1;
43 for(var i = 0; i < str.length * chrsz; i += chrsz)
44 {
45 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
46 }
47 return bin;
48 };
49
50 /*
51 * Convert an array of little-endian words to a string
52 */
53 var binl2str = function (bin) {
54 var str = "";
55 var mask = (1 << chrsz) - 1;
56 for(var i = 0; i < bin.length * 32; i += chrsz)
57 {
58 str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
59 }
60 return str;
61 };
62
63 /*
64 * Convert an array of little-endian words to a hex string.
65 */
66 var binl2hex = function (binarray) {
67 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
68 var str = "";
69 for(var i = 0; i < binarray.length * 4; i++)
70 {
71 str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
72 hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
73 }
74 return str;
75 };
76
77 /*
78 * Convert an array of little-endian words to a base-64 string
79 */
80 var binl2b64 = function (binarray) {
81 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
82 var str = "";
83 var triplet, j;
84 for(var i = 0; i < binarray.length * 4; i += 3)
85 {
86 triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16) |
87 (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 ) |
88 ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
89 for(j = 0; j < 4; j++)
90 {
91 if(i * 8 + j * 6 > binarray.length * 32) { str += b64pad; }
92 else { str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); }
93 }
94 }
95 return str;
96 };
97
98 /*
99 * These functions implement the four basic operations the algorithm uses.
100 */
101 var md5_cmn = function (q, a, b, x, s, t) {
102 return safe_add(bit_rol(safe_add(safe_add(a, q),safe_add(x, t)), s),b);
103 };
104
105 var md5_ff = function (a, b, c, d, x, s, t) {
106 return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
107 };
108
109 var md5_gg = function (a, b, c, d, x, s, t) {
110 return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
111 };
112
113 var md5_hh = function (a, b, c, d, x, s, t) {
114 return md5_cmn(b ^ c ^ d, a, b, x, s, t);
115 };
116
117 var md5_ii = function (a, b, c, d, x, s, t) {
118 return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
119 };
120
121 /*
122 * Calculate the MD5 of an array of little-endian words, and a bit length
123 */
124 var core_md5 = function (x, len) {
125 /* append padding */
126 x[len >> 5] |= 0x80 << ((len) % 32);
127 x[(((len + 64) >>> 9) << 4) + 14] = len;
128
129 var a = 1732584193;
130 var b = -271733879;
131 var c = -1732584194;
132 var d = 271733878;
133
134 var olda, oldb, oldc, oldd;
135 for (var i = 0; i < x.length; i += 16)
136 {
137 olda = a;
138 oldb = b;
139 oldc = c;
140 oldd = d;
141
142 a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
143 d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
144 c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
145 b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
146 a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
147 d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
148 c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
149 b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
150 a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
151 d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
152 c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
153 b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
154 a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
155 d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
156 c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
157 b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
158
159 a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
160 d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
161 c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
162 b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
163 a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
164 d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
165 c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
166 b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
167 a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
168 d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
169 c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
170 b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
171 a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
172 d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
173 c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
174 b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
175
176 a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
177 d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
178 c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
179 b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
180 a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
181 d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
182 c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
183 b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
184 a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
185 d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
186 c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
187 b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
188 a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
189 d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
190 c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
191 b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
192
193 a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
194 d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
195 c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
196 b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
197 a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
198 d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
199 c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
200 b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
201 a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
202 d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
203 c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
204 b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
205 a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
206 d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
207 c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
208 b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
209
210 a = safe_add(a, olda);
211 b = safe_add(b, oldb);
212 c = safe_add(c, oldc);
213 d = safe_add(d, oldd);
214 }
215 return [a, b, c, d];
216 };
217
218
219 /*
220 * Calculate the HMAC-MD5, of a key and some data
221 */
222 var core_hmac_md5 = function (key, data) {
223 var bkey = str2binl(key);
224 if(bkey.length > 16) { bkey = core_md5(bkey, key.length * chrsz); }
225
226 var ipad = new Array(16), opad = new Array(16);
227 for(var i = 0; i < 16; i++)
228 {
229 ipad[i] = bkey[i] ^ 0x36363636;
230 opad[i] = bkey[i] ^ 0x5C5C5C5C;
231 }
232
233 var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
234 return core_md5(opad.concat(hash), 512 + 128);
235 };
236
237 var obj = {
238 /*
239 * These are the functions you'll usually want to call.
240 * They take string arguments and return either hex or base-64 encoded
241 * strings.
242 */
243 hexdigest: function (s) {
244 return binl2hex(core_md5(str2binl(s), s.length * chrsz));
245 },
246
247 b64digest: function (s) {
248 return binl2b64(core_md5(str2binl(s), s.length * chrsz));
249 },
250
251 hash: function (s) {
252 return binl2str(core_md5(str2binl(s), s.length * chrsz));
253 },
254
255 hmac_hexdigest: function (key, data) {
256 return binl2hex(core_hmac_md5(key, data));
257 },
258
259 hmac_b64digest: function (key, data) {
260 return binl2b64(core_hmac_md5(key, data));
261 },
262
263 hmac_hash: function (key, data) {
264 return binl2str(core_hmac_md5(key, data));
265 },
266
267 /*
268 * Perform a simple self-test to see if the VM is working
269 */
270 test: function () {
271 return MD5.hexdigest("abc") === "900150983cd24fb0d6963f7d28e17f72";
272 }
273 };
274
275 return obj;
276 })();