Skip to content
This repository was archived by the owner on Sep 25, 2021. It is now read-only.

Commit 4ce69f7

Browse files
committed
Update code to es6, added rollup and babel
1 parent 9756d82 commit 4ce69f7

File tree

15 files changed

+800
-257
lines changed

15 files changed

+800
-257
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.DS_Store
22
node_modules
3+
.idea/
4+
package-lock.json

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Include the (minified) JavaScript [MD5](https://en.wikipedia.org/wiki/MD5)
1515
script in your HTML markup:
1616

1717
```html
18-
<script src="js/md5.min.js"></script>
18+
<script src="dist/md5.min.js"></script>
1919
```
2020

2121
In your application code, calculate the

dist/es6/md5.js

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
(function (global, factory) {
2+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3+
typeof define === 'function' && define.amd ? define(factory) :
4+
(global.md5 = factory());
5+
}(this, (function () { 'use strict';
6+
7+
/*
8+
* JavaScript MD5
9+
* https://github.com/blueimp/JavaScript-MD5
10+
*
11+
* Copyright 2011, Sebastian Tschan
12+
* https://blueimp.net
13+
*
14+
* Licensed under the MIT license:
15+
* https://opensource.org/licenses/MIT
16+
*
17+
* Based on
18+
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
19+
* Digest Algorithm, as defined in RFC 1321.
20+
* Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
21+
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
22+
* Distributed under the BSD License
23+
* See http://pajhome.org.uk/crypt/md5 for more info.
24+
*/
25+
26+
/*
27+
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
28+
* to work around bugs in some JS interpreters.
29+
*/
30+
const safeAdd = (x, y) => {
31+
let lsw = (x & 0xFFFF) + (y & 0xFFFF);
32+
return (((x >> 16) + (y >> 16) + (lsw >> 16)) << 16) | (lsw & 0xFFFF)
33+
};
34+
35+
/*
36+
* Bitwise rotate a 32-bit number to the left.
37+
*/
38+
const bitRotateLeft = (num, cnt) => (num << cnt) | (num >>> (32 - cnt));
39+
40+
/*
41+
* These functions implement the four basic operations the algorithm uses.
42+
*/
43+
const md5cmn = (q, a, b, x, s, t) => safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
44+
const md5ff = (a, b, c, d, x, s, t) => md5cmn((b & c) | ((~b) & d), a, b, x, s, t);
45+
const md5gg = (a, b, c, d, x, s, t) => md5cmn((b & d) | (c & (~d)), a, b, x, s, t);
46+
const md5hh = (a, b, c, d, x, s, t) => md5cmn(b ^ c ^ d, a, b, x, s, t);
47+
const md5ii = (a, b, c, d, x, s, t) => md5cmn(c ^ (b | (~d)), a, b, x, s, t);
48+
49+
const firstChunk = (chunks, x, i) => {
50+
let [a, b, c, d] = chunks;
51+
a = md5ff(a, b, c, d, x[i + 0], 7, -680876936);
52+
d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
53+
c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
54+
b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
55+
56+
a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
57+
d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
58+
c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
59+
b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
60+
61+
a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
62+
d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
63+
c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
64+
b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
65+
66+
a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
67+
d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
68+
c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
69+
b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
70+
71+
return [a, b, c, d]
72+
};
73+
const secondChunk = (chunks, x, i) => {
74+
let [a, b, c, d] = chunks;
75+
a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
76+
d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
77+
c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
78+
b = md5gg(b, c, d, a, x[i], 20, -373897302);
79+
80+
a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
81+
d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
82+
c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
83+
b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
84+
85+
a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
86+
d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
87+
c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
88+
b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
89+
90+
a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
91+
d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
92+
c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
93+
b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
94+
95+
return [a, b, c, d]
96+
};
97+
const thirdChunk = (chunks, x, i) => {
98+
let [a, b, c, d] = chunks;
99+
a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
100+
d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
101+
c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
102+
b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
103+
104+
a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
105+
d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
106+
c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
107+
b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
108+
109+
a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
110+
d = md5hh(d, a, b, c, x[i], 11, -358537222);
111+
c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
112+
b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
113+
114+
a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
115+
d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
116+
c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
117+
b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
118+
119+
return [a, b, c, d]
120+
};
121+
const fourthChunk = (chunks, x, i) => {
122+
let [a, b, c, d] = chunks;
123+
a = md5ii(a, b, c, d, x[i], 6, -198630844);
124+
d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
125+
c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
126+
b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
127+
128+
a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
129+
d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
130+
c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
131+
b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
132+
133+
a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
134+
d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
135+
c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
136+
b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
137+
138+
a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
139+
d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
140+
c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
141+
b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
142+
return [a, b, c, d]
143+
};
144+
/*
145+
* Calculate the MD5 of an array of little-endian words, and a bit length.
146+
*/
147+
const binlMD5 = (x, len) => {
148+
/* append padding */
149+
x[len >> 5] |= 0x80 << (len % 32);
150+
x[(((len + 64) >>> 9) << 4) + 14] = len;
151+
let commands = [firstChunk, secondChunk, thirdChunk, fourthChunk],
152+
initialChunks = [
153+
1732584193,
154+
-271733879,
155+
-1732584194,
156+
271733878
157+
];
158+
return Array.from({length: Math.floor(x.length / 16) + 1}, (v, i) => i * 16)
159+
.reduce((chunks, i) => commands
160+
.reduce((newChunks, apply) => apply(newChunks, x, i), chunks.slice())
161+
.map((chunk, index) => safeAdd(chunk, chunks[index])), initialChunks)
162+
163+
};
164+
165+
/*
166+
* Convert an array of little-endian words to a string
167+
*/
168+
const binl2rstr = input => Array(input.length * 4).fill(8).reduce((output, k, i) => output + String.fromCharCode((input[(i * k) >> 5] >>> ((i * k) % 32)) & 0xFF), '');
169+
170+
/*
171+
* Convert a raw string to an array of little-endian words
172+
* Characters >255 have their high-byte silently ignored.
173+
*/
174+
const rstr2binl = input => Array.from(input).map(i => i.charCodeAt(0)).reduce((output, cc, i) => {
175+
let resp = output.slice();
176+
resp[(i * 8) >> 5] |= (cc & 0xFF) << ((i * 8) % 32);
177+
return resp
178+
}, []);
179+
180+
/*
181+
* Calculate the MD5 of a raw string
182+
*/
183+
const rstrMD5 = string => binl2rstr(binlMD5(rstr2binl(string), string.length * 8));
184+
/*
185+
* Calculate the HMAC-MD5, of a key and some data (raw strings)
186+
*/
187+
const strHMACMD5 = (key, data) => {
188+
let bkey = rstr2binl(key),
189+
ipad = Array(16).fill(undefined ^ 0x36363636),
190+
opad = Array(16).fill(undefined ^ 0x5C5C5C5C);
191+
192+
if (bkey.length > 16) {
193+
bkey = binlMD5(bkey, key.length * 8);
194+
}
195+
196+
bkey.forEach((k, i) => {
197+
ipad[i] = k ^ 0x36363636;
198+
opad[i] = k ^ 0x5C5C5C5C;
199+
});
200+
201+
return binl2rstr(binlMD5(opad.concat(binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8)), 512 + 128))
202+
};
203+
204+
/*
205+
* Convert a raw string to a hex string
206+
*/
207+
const rstr2hex = input => {
208+
const hexTab = (pos) => '0123456789abcdef'.charAt(pos);
209+
return Array.from(input).map(c => c.charCodeAt(0)).reduce((output, x, i) => output + hexTab((x >>> 4) & 0x0F) + hexTab(x & 0x0F), '')
210+
};
211+
212+
/*
213+
* Encode a string as utf-8
214+
*/
215+
216+
const str2rstrUTF8 = unicodeString => {
217+
if (typeof unicodeString !== 'string') throw new TypeError('parameter ‘unicodeString’ is not a string');
218+
const cc = c => c.charCodeAt(0);
219+
return unicodeString
220+
.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
221+
c => String.fromCharCode(0xc0 | cc(c) >> 6, 0x80 | cc(c) & 0x3f))
222+
.replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
223+
c => String.fromCharCode(0xe0 | cc(c) >> 12, 0x80 | cc(c) >> 6 & 0x3F, 0x80 | cc(c) & 0x3f))
224+
};
225+
226+
/*
227+
* Take string arguments and return either raw or hex encoded strings
228+
*/
229+
const rawMD5 = s => rstrMD5(str2rstrUTF8(s));
230+
231+
const hexMD5 = s => rstr2hex(rawMD5(s));
232+
233+
const rawHMACMD5 = (k, d) => strHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d));
234+
235+
const hexHMACMD5 = (k, d) => rstr2hex(rawHMACMD5(k, d));
236+
237+
238+
var md5 = (string, key, raw) => {
239+
if (!key) {
240+
if (!raw) {
241+
return hexMD5(string)
242+
}
243+
return rawMD5(string)
244+
}
245+
if (!raw) {
246+
return hexHMACMD5(key, string)
247+
}
248+
return rawHMACMD5(key, string)
249+
};
250+
251+
return md5;
252+
253+
})));

dist/es6/md5.min.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/es6/md5.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)