Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80556 views
1
var inherits = require('inherits')
2
var Hash = require('./hash')
3
4
var K = [
5
0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
6
0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
7
0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
8
0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
9
0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
10
0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
11
0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
12
0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
13
0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
14
0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
15
0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
16
0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
17
0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
18
0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
19
0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
20
0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
21
0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
22
0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
23
0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
24
0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
25
0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
26
0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
27
0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
28
0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
29
0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
30
0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
31
0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
32
0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
33
0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
34
0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
35
0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
36
0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
37
0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
38
0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
39
0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
40
0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
41
0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
42
0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
43
0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
44
0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
45
]
46
47
var W = new Array(160)
48
49
function Sha512() {
50
this.init()
51
this._w = W
52
53
Hash.call(this, 128, 112)
54
}
55
56
inherits(Sha512, Hash)
57
58
Sha512.prototype.init = function () {
59
this._a = 0x6a09e667|0
60
this._b = 0xbb67ae85|0
61
this._c = 0x3c6ef372|0
62
this._d = 0xa54ff53a|0
63
this._e = 0x510e527f|0
64
this._f = 0x9b05688c|0
65
this._g = 0x1f83d9ab|0
66
this._h = 0x5be0cd19|0
67
68
this._al = 0xf3bcc908|0
69
this._bl = 0x84caa73b|0
70
this._cl = 0xfe94f82b|0
71
this._dl = 0x5f1d36f1|0
72
this._el = 0xade682d1|0
73
this._fl = 0x2b3e6c1f|0
74
this._gl = 0xfb41bd6b|0
75
this._hl = 0x137e2179|0
76
77
return this
78
}
79
80
function S (X, Xl, n) {
81
return (X >>> n) | (Xl << (32 - n))
82
}
83
84
function Ch (x, y, z) {
85
return ((x & y) ^ ((~x) & z));
86
}
87
88
function Maj (x, y, z) {
89
return ((x & y) ^ (x & z) ^ (y & z));
90
}
91
92
Sha512.prototype._update = function(M) {
93
var W = this._w
94
95
var a = this._a | 0
96
var b = this._b | 0
97
var c = this._c | 0
98
var d = this._d | 0
99
var e = this._e | 0
100
var f = this._f | 0
101
var g = this._g | 0
102
var h = this._h | 0
103
104
var al = this._al | 0
105
var bl = this._bl | 0
106
var cl = this._cl | 0
107
var dl = this._dl | 0
108
var el = this._el | 0
109
var fl = this._fl | 0
110
var gl = this._gl | 0
111
var hl = this._hl | 0
112
113
var i = 0, j = 0
114
var Wi, Wil
115
function calcW() {
116
var x = W[j - 15*2]
117
var xl = W[j - 15*2 + 1]
118
var gamma0 = S(x, xl, 1) ^ S(x, xl, 8) ^ (x >>> 7)
119
var gamma0l = S(xl, x, 1) ^ S(xl, x, 8) ^ S(xl, x, 7)
120
121
x = W[j - 2*2]
122
xl = W[j - 2*2 + 1]
123
var gamma1 = S(x, xl, 19) ^ S(xl, x, 29) ^ (x >>> 6)
124
var gamma1l = S(xl, x, 19) ^ S(x, xl, 29) ^ S(xl, x, 6)
125
126
// W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
127
var Wi7 = W[j - 7*2]
128
var Wi7l = W[j - 7*2 + 1]
129
130
var Wi16 = W[j - 16*2]
131
var Wi16l = W[j - 16*2 + 1]
132
133
Wil = gamma0l + Wi7l
134
Wi = gamma0 + Wi7 + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0)
135
Wil = Wil + gamma1l
136
Wi = Wi + gamma1 + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0)
137
Wil = Wil + Wi16l
138
Wi = Wi + Wi16 + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0)
139
}
140
141
function loop() {
142
W[j] = Wi
143
W[j + 1] = Wil
144
145
var maj = Maj(a, b, c)
146
var majl = Maj(al, bl, cl)
147
148
var sigma0h = S(a, al, 28) ^ S(al, a, 2) ^ S(al, a, 7)
149
var sigma0l = S(al, a, 28) ^ S(a, al, 2) ^ S(a, al, 7)
150
var sigma1h = S(e, el, 14) ^ S(e, el, 18) ^ S(el, e, 9)
151
var sigma1l = S(el, e, 14) ^ S(el, e, 18) ^ S(e, el, 9)
152
153
// t1 = h + sigma1 + ch + K[i] + W[i]
154
var Ki = K[j]
155
var Kil = K[j + 1]
156
157
var ch = Ch(e, f, g)
158
var chl = Ch(el, fl, gl)
159
160
var t1l = hl + sigma1l
161
var t1 = h + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0)
162
t1l = t1l + chl
163
t1 = t1 + ch + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0)
164
t1l = t1l + Kil
165
t1 = t1 + Ki + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0)
166
t1l = t1l + Wil
167
t1 = t1 + Wi + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0)
168
169
// t2 = sigma0 + maj
170
var t2l = sigma0l + majl
171
var t2 = sigma0h + maj + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0)
172
173
h = g
174
hl = gl
175
g = f
176
gl = fl
177
f = e
178
fl = el
179
el = (dl + t1l) | 0
180
e = (d + t1 + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0
181
d = c
182
dl = cl
183
c = b
184
cl = bl
185
b = a
186
bl = al
187
al = (t1l + t2l) | 0
188
a = (t1 + t2 + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0
189
190
i++
191
j += 2
192
}
193
194
while (i < 16) {
195
Wi = M.readInt32BE(j * 4)
196
Wil = M.readInt32BE(j * 4 + 4)
197
198
loop()
199
}
200
201
while (i < 80) {
202
calcW()
203
loop()
204
}
205
206
this._al = (this._al + al) | 0
207
this._bl = (this._bl + bl) | 0
208
this._cl = (this._cl + cl) | 0
209
this._dl = (this._dl + dl) | 0
210
this._el = (this._el + el) | 0
211
this._fl = (this._fl + fl) | 0
212
this._gl = (this._gl + gl) | 0
213
this._hl = (this._hl + hl) | 0
214
215
this._a = (this._a + a + ((this._al >>> 0) < (al >>> 0) ? 1 : 0)) | 0
216
this._b = (this._b + b + ((this._bl >>> 0) < (bl >>> 0) ? 1 : 0)) | 0
217
this._c = (this._c + c + ((this._cl >>> 0) < (cl >>> 0) ? 1 : 0)) | 0
218
this._d = (this._d + d + ((this._dl >>> 0) < (dl >>> 0) ? 1 : 0)) | 0
219
this._e = (this._e + e + ((this._el >>> 0) < (el >>> 0) ? 1 : 0)) | 0
220
this._f = (this._f + f + ((this._fl >>> 0) < (fl >>> 0) ? 1 : 0)) | 0
221
this._g = (this._g + g + ((this._gl >>> 0) < (gl >>> 0) ? 1 : 0)) | 0
222
this._h = (this._h + h + ((this._hl >>> 0) < (hl >>> 0) ? 1 : 0)) | 0
223
}
224
225
Sha512.prototype._hash = function () {
226
var H = new Buffer(64)
227
228
function writeInt64BE(h, l, offset) {
229
H.writeInt32BE(h, offset)
230
H.writeInt32BE(l, offset + 4)
231
}
232
233
writeInt64BE(this._a, this._al, 0)
234
writeInt64BE(this._b, this._bl, 8)
235
writeInt64BE(this._c, this._cl, 16)
236
writeInt64BE(this._d, this._dl, 24)
237
writeInt64BE(this._e, this._el, 32)
238
writeInt64BE(this._f, this._fl, 40)
239
writeInt64BE(this._g, this._gl, 48)
240
writeInt64BE(this._h, this._hl, 56)
241
242
return H
243
}
244
245
module.exports = Sha512
246
247