1 | var hashUtils = require('./browserHashUtils');
|
2 | var Buffer = require('buffer/').Buffer;
|
3 |
|
4 | var BLOCK_SIZE = 64;
|
5 |
|
6 | var DIGEST_LENGTH = 16;
|
7 |
|
8 | var INIT = [
|
9 | 0x67452301,
|
10 | 0xefcdab89,
|
11 | 0x98badcfe,
|
12 | 0x10325476,
|
13 | ];
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function Md5() {
|
19 | this.state = [
|
20 | 0x67452301,
|
21 | 0xefcdab89,
|
22 | 0x98badcfe,
|
23 | 0x10325476,
|
24 | ];
|
25 | this.buffer = new DataView(new ArrayBuffer(BLOCK_SIZE));
|
26 | this.bufferLength = 0;
|
27 | this.bytesHashed = 0;
|
28 | this.finished = false;
|
29 | }
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | module.exports = exports = Md5;
|
35 |
|
36 | Md5.BLOCK_SIZE = BLOCK_SIZE;
|
37 |
|
38 | Md5.prototype.update = function (sourceData) {
|
39 | if (hashUtils.isEmptyData(sourceData)) {
|
40 | return this;
|
41 | } else if (this.finished) {
|
42 | throw new Error('Attempted to update an already finished hash.');
|
43 | }
|
44 |
|
45 | var data = hashUtils.convertToBuffer(sourceData);
|
46 | var position = 0;
|
47 | var byteLength = data.byteLength;
|
48 | this.bytesHashed += byteLength;
|
49 | while (byteLength > 0) {
|
50 | this.buffer.setUint8(this.bufferLength++, data[position++]);
|
51 | byteLength--;
|
52 | if (this.bufferLength === BLOCK_SIZE) {
|
53 | this.hashBuffer();
|
54 | this.bufferLength = 0;
|
55 | }
|
56 | }
|
57 |
|
58 | return this;
|
59 | };
|
60 |
|
61 | Md5.prototype.digest = function (encoding) {
|
62 | if (!this.finished) {
|
63 | var _a = this, buffer = _a.buffer, undecoratedLength = _a.bufferLength, bytesHashed = _a.bytesHashed;
|
64 | var bitsHashed = bytesHashed * 8;
|
65 | buffer.setUint8(this.bufferLength++, 128);
|
66 |
|
67 | if (undecoratedLength % BLOCK_SIZE >= BLOCK_SIZE - 8) {
|
68 | for (var i = this.bufferLength; i < BLOCK_SIZE; i++) {
|
69 | buffer.setUint8(i, 0);
|
70 | }
|
71 | this.hashBuffer();
|
72 | this.bufferLength = 0;
|
73 | }
|
74 | for (var i = this.bufferLength; i < BLOCK_SIZE - 8; i++) {
|
75 | buffer.setUint8(i, 0);
|
76 | }
|
77 | buffer.setUint32(BLOCK_SIZE - 8, bitsHashed >>> 0, true);
|
78 | buffer.setUint32(BLOCK_SIZE - 4, Math.floor(bitsHashed / 0x100000000), true);
|
79 | this.hashBuffer();
|
80 | this.finished = true;
|
81 | }
|
82 | var out = new DataView(new ArrayBuffer(DIGEST_LENGTH));
|
83 | for (var i = 0; i < 4; i++) {
|
84 | out.setUint32(i * 4, this.state[i], true);
|
85 | }
|
86 | var buff = new Buffer(out.buffer, out.byteOffset, out.byteLength);
|
87 | return encoding ? buff.toString(encoding) : buff;
|
88 | };
|
89 |
|
90 | Md5.prototype.hashBuffer = function () {
|
91 | var _a = this, buffer = _a.buffer, state = _a.state;
|
92 | var a = state[0], b = state[1], c = state[2], d = state[3];
|
93 | a = ff(a, b, c, d, buffer.getUint32(0, true), 7, 0xd76aa478);
|
94 | d = ff(d, a, b, c, buffer.getUint32(4, true), 12, 0xe8c7b756);
|
95 | c = ff(c, d, a, b, buffer.getUint32(8, true), 17, 0x242070db);
|
96 | b = ff(b, c, d, a, buffer.getUint32(12, true), 22, 0xc1bdceee);
|
97 | a = ff(a, b, c, d, buffer.getUint32(16, true), 7, 0xf57c0faf);
|
98 | d = ff(d, a, b, c, buffer.getUint32(20, true), 12, 0x4787c62a);
|
99 | c = ff(c, d, a, b, buffer.getUint32(24, true), 17, 0xa8304613);
|
100 | b = ff(b, c, d, a, buffer.getUint32(28, true), 22, 0xfd469501);
|
101 | a = ff(a, b, c, d, buffer.getUint32(32, true), 7, 0x698098d8);
|
102 | d = ff(d, a, b, c, buffer.getUint32(36, true), 12, 0x8b44f7af);
|
103 | c = ff(c, d, a, b, buffer.getUint32(40, true), 17, 0xffff5bb1);
|
104 | b = ff(b, c, d, a, buffer.getUint32(44, true), 22, 0x895cd7be);
|
105 | a = ff(a, b, c, d, buffer.getUint32(48, true), 7, 0x6b901122);
|
106 | d = ff(d, a, b, c, buffer.getUint32(52, true), 12, 0xfd987193);
|
107 | c = ff(c, d, a, b, buffer.getUint32(56, true), 17, 0xa679438e);
|
108 | b = ff(b, c, d, a, buffer.getUint32(60, true), 22, 0x49b40821);
|
109 | a = gg(a, b, c, d, buffer.getUint32(4, true), 5, 0xf61e2562);
|
110 | d = gg(d, a, b, c, buffer.getUint32(24, true), 9, 0xc040b340);
|
111 | c = gg(c, d, a, b, buffer.getUint32(44, true), 14, 0x265e5a51);
|
112 | b = gg(b, c, d, a, buffer.getUint32(0, true), 20, 0xe9b6c7aa);
|
113 | a = gg(a, b, c, d, buffer.getUint32(20, true), 5, 0xd62f105d);
|
114 | d = gg(d, a, b, c, buffer.getUint32(40, true), 9, 0x02441453);
|
115 | c = gg(c, d, a, b, buffer.getUint32(60, true), 14, 0xd8a1e681);
|
116 | b = gg(b, c, d, a, buffer.getUint32(16, true), 20, 0xe7d3fbc8);
|
117 | a = gg(a, b, c, d, buffer.getUint32(36, true), 5, 0x21e1cde6);
|
118 | d = gg(d, a, b, c, buffer.getUint32(56, true), 9, 0xc33707d6);
|
119 | c = gg(c, d, a, b, buffer.getUint32(12, true), 14, 0xf4d50d87);
|
120 | b = gg(b, c, d, a, buffer.getUint32(32, true), 20, 0x455a14ed);
|
121 | a = gg(a, b, c, d, buffer.getUint32(52, true), 5, 0xa9e3e905);
|
122 | d = gg(d, a, b, c, buffer.getUint32(8, true), 9, 0xfcefa3f8);
|
123 | c = gg(c, d, a, b, buffer.getUint32(28, true), 14, 0x676f02d9);
|
124 | b = gg(b, c, d, a, buffer.getUint32(48, true), 20, 0x8d2a4c8a);
|
125 | a = hh(a, b, c, d, buffer.getUint32(20, true), 4, 0xfffa3942);
|
126 | d = hh(d, a, b, c, buffer.getUint32(32, true), 11, 0x8771f681);
|
127 | c = hh(c, d, a, b, buffer.getUint32(44, true), 16, 0x6d9d6122);
|
128 | b = hh(b, c, d, a, buffer.getUint32(56, true), 23, 0xfde5380c);
|
129 | a = hh(a, b, c, d, buffer.getUint32(4, true), 4, 0xa4beea44);
|
130 | d = hh(d, a, b, c, buffer.getUint32(16, true), 11, 0x4bdecfa9);
|
131 | c = hh(c, d, a, b, buffer.getUint32(28, true), 16, 0xf6bb4b60);
|
132 | b = hh(b, c, d, a, buffer.getUint32(40, true), 23, 0xbebfbc70);
|
133 | a = hh(a, b, c, d, buffer.getUint32(52, true), 4, 0x289b7ec6);
|
134 | d = hh(d, a, b, c, buffer.getUint32(0, true), 11, 0xeaa127fa);
|
135 | c = hh(c, d, a, b, buffer.getUint32(12, true), 16, 0xd4ef3085);
|
136 | b = hh(b, c, d, a, buffer.getUint32(24, true), 23, 0x04881d05);
|
137 | a = hh(a, b, c, d, buffer.getUint32(36, true), 4, 0xd9d4d039);
|
138 | d = hh(d, a, b, c, buffer.getUint32(48, true), 11, 0xe6db99e5);
|
139 | c = hh(c, d, a, b, buffer.getUint32(60, true), 16, 0x1fa27cf8);
|
140 | b = hh(b, c, d, a, buffer.getUint32(8, true), 23, 0xc4ac5665);
|
141 | a = ii(a, b, c, d, buffer.getUint32(0, true), 6, 0xf4292244);
|
142 | d = ii(d, a, b, c, buffer.getUint32(28, true), 10, 0x432aff97);
|
143 | c = ii(c, d, a, b, buffer.getUint32(56, true), 15, 0xab9423a7);
|
144 | b = ii(b, c, d, a, buffer.getUint32(20, true), 21, 0xfc93a039);
|
145 | a = ii(a, b, c, d, buffer.getUint32(48, true), 6, 0x655b59c3);
|
146 | d = ii(d, a, b, c, buffer.getUint32(12, true), 10, 0x8f0ccc92);
|
147 | c = ii(c, d, a, b, buffer.getUint32(40, true), 15, 0xffeff47d);
|
148 | b = ii(b, c, d, a, buffer.getUint32(4, true), 21, 0x85845dd1);
|
149 | a = ii(a, b, c, d, buffer.getUint32(32, true), 6, 0x6fa87e4f);
|
150 | d = ii(d, a, b, c, buffer.getUint32(60, true), 10, 0xfe2ce6e0);
|
151 | c = ii(c, d, a, b, buffer.getUint32(24, true), 15, 0xa3014314);
|
152 | b = ii(b, c, d, a, buffer.getUint32(52, true), 21, 0x4e0811a1);
|
153 | a = ii(a, b, c, d, buffer.getUint32(16, true), 6, 0xf7537e82);
|
154 | d = ii(d, a, b, c, buffer.getUint32(44, true), 10, 0xbd3af235);
|
155 | c = ii(c, d, a, b, buffer.getUint32(8, true), 15, 0x2ad7d2bb);
|
156 | b = ii(b, c, d, a, buffer.getUint32(36, true), 21, 0xeb86d391);
|
157 | state[0] = (a + state[0]) & 0xFFFFFFFF;
|
158 | state[1] = (b + state[1]) & 0xFFFFFFFF;
|
159 | state[2] = (c + state[2]) & 0xFFFFFFFF;
|
160 | state[3] = (d + state[3]) & 0xFFFFFFFF;
|
161 | };
|
162 |
|
163 | function cmn(q, a, b, x, s, t) {
|
164 | a = (((a + q) & 0xFFFFFFFF) + ((x + t) & 0xFFFFFFFF)) & 0xFFFFFFFF;
|
165 | return (((a << s) | (a >>> (32 - s))) + b) & 0xFFFFFFFF;
|
166 | }
|
167 |
|
168 | function ff(a, b, c, d, x, s, t) {
|
169 | return cmn((b & c) | ((~b) & d), a, b, x, s, t);
|
170 | }
|
171 |
|
172 | function gg(a, b, c, d, x, s, t) {
|
173 | return cmn((b & d) | (c & (~d)), a, b, x, s, t);
|
174 | }
|
175 |
|
176 | function hh(a, b, c, d, x, s, t) {
|
177 | return cmn(b ^ c ^ d, a, b, x, s, t);
|
178 | }
|
179 |
|
180 | function ii(a, b, c, d, x, s, t) {
|
181 | return cmn(c ^ (b | (~d)), a, b, x, s, t);
|
182 | }
|