1export function md5(input) {
2 /*
3 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
16 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
17 /*
18 * These are the functions you'll usually want to call
19 * They take string arguments and return either hex or base-64 encoded strings
20 */
21 function hex_md5(s) {
22 return binl2hex(core_md5(str2binl(s), s.length * chrsz));
23 }
24 function b64_md5(s) {
25 return binl2b64(core_md5(str2binl(s), s.length * chrsz));
26 }
27 function str_md5(s) {
28 return binl2str(core_md5(str2binl(s), s.length * chrsz));
29 }
30 function hex_hmac_md5(key, data) {
31 return binl2hex(core_hmac_md5(key, data));
32 }
33 function b64_hmac_md5(key, data) {
34 return binl2b64(core_hmac_md5(key, data));
35 }
36 function str_hmac_md5(key, data) {
37 return binl2str(core_hmac_md5(key, data));
38 }
40 * Perform a simple self-test to see if the VM is working
41 */
42 function md5_vm_test() {
43 return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
44 }
46 * Calculate the MD5 of an array of little-endian words, and a bit length
47 */
48 function core_md5(x, len) {
49 /* append padding */
50 x[len >> 5] |= 0x80 << ((len) % 32);
131 }
132 /*
133 * These functions implement the four basic operations the algorithm uses.
134 */
135 function md5_cmn(q, a, b, x, s, t) {
136 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
137 }
138 function md5_ff(a, b, c, d, x, s, t) {
139 return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
140 }
141 function md5_gg(a, b, c, d, x, s, t) {
142 return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
143 }
144 function md5_hh(a, b, c, d, x, s, t) {
145 return md5_cmn(b ^ c ^ d, a, b, x, s, t);
146 }
147 function md5_ii(a, b, c, d, x, s, t) {
148 return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
149 }
151 * Calculate the HMAC-MD5, of a key and some data
152 */
153 function core_hmac_md5(key, data) {
154 var bkey = str2binl(key);
155 if (bkey.length > 16)
167 * to work around bugs in some JS interpreters.
168 */
169 function safe_add(x, y) {
170 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
171 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
175 * Bitwise rotate a 32-bit number to the left.
176 */
177 function bit_rol(num, cnt) {
178 return (num << cnt) | (num >>> (32 - cnt));
179 }
182 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
183 */
184 function str2binl(str) {
185 var bin = Array();
186 var mask = (1 << chrsz) - 1;
192 * Convert an array of little-endian words to a string
193 */
194 function binl2str(bin) {
195 var str = "";
196 var mask = (1 << chrsz) - 1;
202 * Convert an array of little-endian words to a hex string.
203 */
204 function binl2hex(binarray) {
205 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
206 var str = "";
214 * Convert an array of little-endian words to a base-64 string
215 */
216 function binl2b64(binarray) {
217 var tab =
218 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";