Projekt

Obecné

Profil

Stáhnout (7.01 KB) Statistiky
| Větev: | Revize:
1
var inherits = require('inherits')
2
var Hash = require('./hash')
3
var Buffer = require('safe-buffer').Buffer
4

    
5
var K = [
6
  0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
7
  0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
8
  0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
9
  0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
10
  0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
11
  0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
12
  0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
13
  0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
14
  0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
15
  0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
16
  0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
17
  0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
18
  0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
19
  0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
20
  0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
21
  0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
22
  0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
23
  0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
24
  0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
25
  0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
26
  0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
27
  0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
28
  0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
29
  0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
30
  0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
31
  0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
32
  0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
33
  0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
34
  0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
35
  0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
36
  0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
37
  0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
38
  0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
39
  0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
40
  0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
41
  0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
42
  0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
43
  0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
44
  0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
45
  0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
46
]
47

    
48
var W = new Array(160)
49

    
50
function Sha512 () {
51
  this.init()
52
  this._w = W
53

    
54
  Hash.call(this, 128, 112)
55
}
56

    
57
inherits(Sha512, Hash)
58

    
59
Sha512.prototype.init = function () {
60
  this._ah = 0x6a09e667
61
  this._bh = 0xbb67ae85
62
  this._ch = 0x3c6ef372
63
  this._dh = 0xa54ff53a
64
  this._eh = 0x510e527f
65
  this._fh = 0x9b05688c
66
  this._gh = 0x1f83d9ab
67
  this._hh = 0x5be0cd19
68

    
69
  this._al = 0xf3bcc908
70
  this._bl = 0x84caa73b
71
  this._cl = 0xfe94f82b
72
  this._dl = 0x5f1d36f1
73
  this._el = 0xade682d1
74
  this._fl = 0x2b3e6c1f
75
  this._gl = 0xfb41bd6b
76
  this._hl = 0x137e2179
77

    
78
  return this
79
}
80

    
81
function Ch (x, y, z) {
82
  return z ^ (x & (y ^ z))
83
}
84

    
85
function maj (x, y, z) {
86
  return (x & y) | (z & (x | y))
87
}
88

    
89
function sigma0 (x, xl) {
90
  return (x >>> 28 | xl << 4) ^ (xl >>> 2 | x << 30) ^ (xl >>> 7 | x << 25)
91
}
92

    
93
function sigma1 (x, xl) {
94
  return (x >>> 14 | xl << 18) ^ (x >>> 18 | xl << 14) ^ (xl >>> 9 | x << 23)
95
}
96

    
97
function Gamma0 (x, xl) {
98
  return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7)
99
}
100

    
101
function Gamma0l (x, xl) {
102
  return (x >>> 1 | xl << 31) ^ (x >>> 8 | xl << 24) ^ (x >>> 7 | xl << 25)
103
}
104

    
105
function Gamma1 (x, xl) {
106
  return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6)
107
}
108

    
109
function Gamma1l (x, xl) {
110
  return (x >>> 19 | xl << 13) ^ (xl >>> 29 | x << 3) ^ (x >>> 6 | xl << 26)
111
}
112

    
113
function getCarry (a, b) {
114
  return (a >>> 0) < (b >>> 0) ? 1 : 0
115
}
116

    
117
Sha512.prototype._update = function (M) {
118
  var W = this._w
119

    
120
  var ah = this._ah | 0
121
  var bh = this._bh | 0
122
  var ch = this._ch | 0
123
  var dh = this._dh | 0
124
  var eh = this._eh | 0
125
  var fh = this._fh | 0
126
  var gh = this._gh | 0
127
  var hh = this._hh | 0
128

    
129
  var al = this._al | 0
130
  var bl = this._bl | 0
131
  var cl = this._cl | 0
132
  var dl = this._dl | 0
133
  var el = this._el | 0
134
  var fl = this._fl | 0
135
  var gl = this._gl | 0
136
  var hl = this._hl | 0
137

    
138
  for (var i = 0; i < 32; i += 2) {
139
    W[i] = M.readInt32BE(i * 4)
140
    W[i + 1] = M.readInt32BE(i * 4 + 4)
141
  }
142
  for (; i < 160; i += 2) {
143
    var xh = W[i - 15 * 2]
144
    var xl = W[i - 15 * 2 + 1]
145
    var gamma0 = Gamma0(xh, xl)
146
    var gamma0l = Gamma0l(xl, xh)
147

    
148
    xh = W[i - 2 * 2]
149
    xl = W[i - 2 * 2 + 1]
150
    var gamma1 = Gamma1(xh, xl)
151
    var gamma1l = Gamma1l(xl, xh)
152

    
153
    // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
154
    var Wi7h = W[i - 7 * 2]
155
    var Wi7l = W[i - 7 * 2 + 1]
156

    
157
    var Wi16h = W[i - 16 * 2]
158
    var Wi16l = W[i - 16 * 2 + 1]
159

    
160
    var Wil = (gamma0l + Wi7l) | 0
161
    var Wih = (gamma0 + Wi7h + getCarry(Wil, gamma0l)) | 0
162
    Wil = (Wil + gamma1l) | 0
163
    Wih = (Wih + gamma1 + getCarry(Wil, gamma1l)) | 0
164
    Wil = (Wil + Wi16l) | 0
165
    Wih = (Wih + Wi16h + getCarry(Wil, Wi16l)) | 0
166

    
167
    W[i] = Wih
168
    W[i + 1] = Wil
169
  }
170

    
171
  for (var j = 0; j < 160; j += 2) {
172
    Wih = W[j]
173
    Wil = W[j + 1]
174

    
175
    var majh = maj(ah, bh, ch)
176
    var majl = maj(al, bl, cl)
177

    
178
    var sigma0h = sigma0(ah, al)
179
    var sigma0l = sigma0(al, ah)
180
    var sigma1h = sigma1(eh, el)
181
    var sigma1l = sigma1(el, eh)
182

    
183
    // t1 = h + sigma1 + ch + K[j] + W[j]
184
    var Kih = K[j]
185
    var Kil = K[j + 1]
186

    
187
    var chh = Ch(eh, fh, gh)
188
    var chl = Ch(el, fl, gl)
189

    
190
    var t1l = (hl + sigma1l) | 0
191
    var t1h = (hh + sigma1h + getCarry(t1l, hl)) | 0
192
    t1l = (t1l + chl) | 0
193
    t1h = (t1h + chh + getCarry(t1l, chl)) | 0
194
    t1l = (t1l + Kil) | 0
195
    t1h = (t1h + Kih + getCarry(t1l, Kil)) | 0
196
    t1l = (t1l + Wil) | 0
197
    t1h = (t1h + Wih + getCarry(t1l, Wil)) | 0
198

    
199
    // t2 = sigma0 + maj
200
    var t2l = (sigma0l + majl) | 0
201
    var t2h = (sigma0h + majh + getCarry(t2l, sigma0l)) | 0
202

    
203
    hh = gh
204
    hl = gl
205
    gh = fh
206
    gl = fl
207
    fh = eh
208
    fl = el
209
    el = (dl + t1l) | 0
210
    eh = (dh + t1h + getCarry(el, dl)) | 0
211
    dh = ch
212
    dl = cl
213
    ch = bh
214
    cl = bl
215
    bh = ah
216
    bl = al
217
    al = (t1l + t2l) | 0
218
    ah = (t1h + t2h + getCarry(al, t1l)) | 0
219
  }
220

    
221
  this._al = (this._al + al) | 0
222
  this._bl = (this._bl + bl) | 0
223
  this._cl = (this._cl + cl) | 0
224
  this._dl = (this._dl + dl) | 0
225
  this._el = (this._el + el) | 0
226
  this._fl = (this._fl + fl) | 0
227
  this._gl = (this._gl + gl) | 0
228
  this._hl = (this._hl + hl) | 0
229

    
230
  this._ah = (this._ah + ah + getCarry(this._al, al)) | 0
231
  this._bh = (this._bh + bh + getCarry(this._bl, bl)) | 0
232
  this._ch = (this._ch + ch + getCarry(this._cl, cl)) | 0
233
  this._dh = (this._dh + dh + getCarry(this._dl, dl)) | 0
234
  this._eh = (this._eh + eh + getCarry(this._el, el)) | 0
235
  this._fh = (this._fh + fh + getCarry(this._fl, fl)) | 0
236
  this._gh = (this._gh + gh + getCarry(this._gl, gl)) | 0
237
  this._hh = (this._hh + hh + getCarry(this._hl, hl)) | 0
238
}
239

    
240
Sha512.prototype._hash = function () {
241
  var H = Buffer.allocUnsafe(64)
242

    
243
  function writeInt64BE (h, l, offset) {
244
    H.writeInt32BE(h, offset)
245
    H.writeInt32BE(l, offset + 4)
246
  }
247

    
248
  writeInt64BE(this._ah, this._al, 0)
249
  writeInt64BE(this._bh, this._bl, 8)
250
  writeInt64BE(this._ch, this._cl, 16)
251
  writeInt64BE(this._dh, this._dl, 24)
252
  writeInt64BE(this._eh, this._el, 32)
253
  writeInt64BE(this._fh, this._fl, 40)
254
  writeInt64BE(this._gh, this._gl, 48)
255
  writeInt64BE(this._hh, this._hl, 56)
256

    
257
  return H
258
}
259

    
260
module.exports = Sha512
(13-13/13)