1 |
3a515b92
|
cagy
|
'use strict'
|
2 |
|
|
var inherits = require('inherits')
|
3 |
|
|
var HashBase = require('hash-base')
|
4 |
|
|
var Buffer = require('safe-buffer').Buffer
|
5 |
|
|
|
6 |
|
|
var ARRAY16 = new Array(16)
|
7 |
|
|
|
8 |
|
|
function MD5 () {
|
9 |
|
|
HashBase.call(this, 64)
|
10 |
|
|
|
11 |
|
|
// state
|
12 |
|
|
this._a = 0x67452301
|
13 |
|
|
this._b = 0xefcdab89
|
14 |
|
|
this._c = 0x98badcfe
|
15 |
|
|
this._d = 0x10325476
|
16 |
|
|
}
|
17 |
|
|
|
18 |
|
|
inherits(MD5, HashBase)
|
19 |
|
|
|
20 |
|
|
MD5.prototype._update = function () {
|
21 |
|
|
var M = ARRAY16
|
22 |
|
|
for (var i = 0; i < 16; ++i) M[i] = this._block.readInt32LE(i * 4)
|
23 |
|
|
|
24 |
|
|
var a = this._a
|
25 |
|
|
var b = this._b
|
26 |
|
|
var c = this._c
|
27 |
|
|
var d = this._d
|
28 |
|
|
|
29 |
|
|
a = fnF(a, b, c, d, M[0], 0xd76aa478, 7)
|
30 |
|
|
d = fnF(d, a, b, c, M[1], 0xe8c7b756, 12)
|
31 |
|
|
c = fnF(c, d, a, b, M[2], 0x242070db, 17)
|
32 |
|
|
b = fnF(b, c, d, a, M[3], 0xc1bdceee, 22)
|
33 |
|
|
a = fnF(a, b, c, d, M[4], 0xf57c0faf, 7)
|
34 |
|
|
d = fnF(d, a, b, c, M[5], 0x4787c62a, 12)
|
35 |
|
|
c = fnF(c, d, a, b, M[6], 0xa8304613, 17)
|
36 |
|
|
b = fnF(b, c, d, a, M[7], 0xfd469501, 22)
|
37 |
|
|
a = fnF(a, b, c, d, M[8], 0x698098d8, 7)
|
38 |
|
|
d = fnF(d, a, b, c, M[9], 0x8b44f7af, 12)
|
39 |
|
|
c = fnF(c, d, a, b, M[10], 0xffff5bb1, 17)
|
40 |
|
|
b = fnF(b, c, d, a, M[11], 0x895cd7be, 22)
|
41 |
|
|
a = fnF(a, b, c, d, M[12], 0x6b901122, 7)
|
42 |
|
|
d = fnF(d, a, b, c, M[13], 0xfd987193, 12)
|
43 |
|
|
c = fnF(c, d, a, b, M[14], 0xa679438e, 17)
|
44 |
|
|
b = fnF(b, c, d, a, M[15], 0x49b40821, 22)
|
45 |
|
|
|
46 |
|
|
a = fnG(a, b, c, d, M[1], 0xf61e2562, 5)
|
47 |
|
|
d = fnG(d, a, b, c, M[6], 0xc040b340, 9)
|
48 |
|
|
c = fnG(c, d, a, b, M[11], 0x265e5a51, 14)
|
49 |
|
|
b = fnG(b, c, d, a, M[0], 0xe9b6c7aa, 20)
|
50 |
|
|
a = fnG(a, b, c, d, M[5], 0xd62f105d, 5)
|
51 |
|
|
d = fnG(d, a, b, c, M[10], 0x02441453, 9)
|
52 |
|
|
c = fnG(c, d, a, b, M[15], 0xd8a1e681, 14)
|
53 |
|
|
b = fnG(b, c, d, a, M[4], 0xe7d3fbc8, 20)
|
54 |
|
|
a = fnG(a, b, c, d, M[9], 0x21e1cde6, 5)
|
55 |
|
|
d = fnG(d, a, b, c, M[14], 0xc33707d6, 9)
|
56 |
|
|
c = fnG(c, d, a, b, M[3], 0xf4d50d87, 14)
|
57 |
|
|
b = fnG(b, c, d, a, M[8], 0x455a14ed, 20)
|
58 |
|
|
a = fnG(a, b, c, d, M[13], 0xa9e3e905, 5)
|
59 |
|
|
d = fnG(d, a, b, c, M[2], 0xfcefa3f8, 9)
|
60 |
|
|
c = fnG(c, d, a, b, M[7], 0x676f02d9, 14)
|
61 |
|
|
b = fnG(b, c, d, a, M[12], 0x8d2a4c8a, 20)
|
62 |
|
|
|
63 |
|
|
a = fnH(a, b, c, d, M[5], 0xfffa3942, 4)
|
64 |
|
|
d = fnH(d, a, b, c, M[8], 0x8771f681, 11)
|
65 |
|
|
c = fnH(c, d, a, b, M[11], 0x6d9d6122, 16)
|
66 |
|
|
b = fnH(b, c, d, a, M[14], 0xfde5380c, 23)
|
67 |
|
|
a = fnH(a, b, c, d, M[1], 0xa4beea44, 4)
|
68 |
|
|
d = fnH(d, a, b, c, M[4], 0x4bdecfa9, 11)
|
69 |
|
|
c = fnH(c, d, a, b, M[7], 0xf6bb4b60, 16)
|
70 |
|
|
b = fnH(b, c, d, a, M[10], 0xbebfbc70, 23)
|
71 |
|
|
a = fnH(a, b, c, d, M[13], 0x289b7ec6, 4)
|
72 |
|
|
d = fnH(d, a, b, c, M[0], 0xeaa127fa, 11)
|
73 |
|
|
c = fnH(c, d, a, b, M[3], 0xd4ef3085, 16)
|
74 |
|
|
b = fnH(b, c, d, a, M[6], 0x04881d05, 23)
|
75 |
|
|
a = fnH(a, b, c, d, M[9], 0xd9d4d039, 4)
|
76 |
|
|
d = fnH(d, a, b, c, M[12], 0xe6db99e5, 11)
|
77 |
|
|
c = fnH(c, d, a, b, M[15], 0x1fa27cf8, 16)
|
78 |
|
|
b = fnH(b, c, d, a, M[2], 0xc4ac5665, 23)
|
79 |
|
|
|
80 |
|
|
a = fnI(a, b, c, d, M[0], 0xf4292244, 6)
|
81 |
|
|
d = fnI(d, a, b, c, M[7], 0x432aff97, 10)
|
82 |
|
|
c = fnI(c, d, a, b, M[14], 0xab9423a7, 15)
|
83 |
|
|
b = fnI(b, c, d, a, M[5], 0xfc93a039, 21)
|
84 |
|
|
a = fnI(a, b, c, d, M[12], 0x655b59c3, 6)
|
85 |
|
|
d = fnI(d, a, b, c, M[3], 0x8f0ccc92, 10)
|
86 |
|
|
c = fnI(c, d, a, b, M[10], 0xffeff47d, 15)
|
87 |
|
|
b = fnI(b, c, d, a, M[1], 0x85845dd1, 21)
|
88 |
|
|
a = fnI(a, b, c, d, M[8], 0x6fa87e4f, 6)
|
89 |
|
|
d = fnI(d, a, b, c, M[15], 0xfe2ce6e0, 10)
|
90 |
|
|
c = fnI(c, d, a, b, M[6], 0xa3014314, 15)
|
91 |
|
|
b = fnI(b, c, d, a, M[13], 0x4e0811a1, 21)
|
92 |
|
|
a = fnI(a, b, c, d, M[4], 0xf7537e82, 6)
|
93 |
|
|
d = fnI(d, a, b, c, M[11], 0xbd3af235, 10)
|
94 |
|
|
c = fnI(c, d, a, b, M[2], 0x2ad7d2bb, 15)
|
95 |
|
|
b = fnI(b, c, d, a, M[9], 0xeb86d391, 21)
|
96 |
|
|
|
97 |
|
|
this._a = (this._a + a) | 0
|
98 |
|
|
this._b = (this._b + b) | 0
|
99 |
|
|
this._c = (this._c + c) | 0
|
100 |
|
|
this._d = (this._d + d) | 0
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
MD5.prototype._digest = function () {
|
104 |
|
|
// create padding and handle blocks
|
105 |
|
|
this._block[this._blockOffset++] = 0x80
|
106 |
|
|
if (this._blockOffset > 56) {
|
107 |
|
|
this._block.fill(0, this._blockOffset, 64)
|
108 |
|
|
this._update()
|
109 |
|
|
this._blockOffset = 0
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
this._block.fill(0, this._blockOffset, 56)
|
113 |
|
|
this._block.writeUInt32LE(this._length[0], 56)
|
114 |
|
|
this._block.writeUInt32LE(this._length[1], 60)
|
115 |
|
|
this._update()
|
116 |
|
|
|
117 |
|
|
// produce result
|
118 |
|
|
var buffer = Buffer.allocUnsafe(16)
|
119 |
|
|
buffer.writeInt32LE(this._a, 0)
|
120 |
|
|
buffer.writeInt32LE(this._b, 4)
|
121 |
|
|
buffer.writeInt32LE(this._c, 8)
|
122 |
|
|
buffer.writeInt32LE(this._d, 12)
|
123 |
|
|
return buffer
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
function rotl (x, n) {
|
127 |
|
|
return (x << n) | (x >>> (32 - n))
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
function fnF (a, b, c, d, m, k, s) {
|
131 |
|
|
return (rotl((a + ((b & c) | ((~b) & d)) + m + k) | 0, s) + b) | 0
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
function fnG (a, b, c, d, m, k, s) {
|
135 |
|
|
return (rotl((a + ((b & d) | (c & (~d))) + m + k) | 0, s) + b) | 0
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
function fnH (a, b, c, d, m, k, s) {
|
139 |
|
|
return (rotl((a + (b ^ c ^ d) + m + k) | 0, s) + b) | 0
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
function fnI (a, b, c, d, m, k, s) {
|
143 |
|
|
return (rotl((a + ((c ^ (b | (~d)))) + m + k) | 0, s) + b) | 0
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
module.exports = MD5
|