235
buff[2] = buff[SHA1Constant.BLOCK_SIZE + 2];
236
}
238
>
return buffLen;
239
>
}
240
241
public digest(): string {
242
>
if (!this._finished) {
hash.ts
243
>
this._finished = true;
244
>
if (this._leftoverHighSurrogate) {
245
// illegal => unicode replacement character
246
this._leftoverHighSurrogate = 0;
247
this._buffLen = this._push(this._buff, this._buffLen, SHA1Constant.UNICODE_REPLACEMENT);
248
}
249
>
this._totalLen += this._buffLen;
hash.ts
250
>
this._wrapUp();
251
>
}
252
>
253
>
return toHexString(this._h0) + toHexString(this._h1) + toHexString(this._h2) + toHexString(this._h3) + toHexString(this._h4);
254
>
}
255
256
private _wrapUp(): void {
257
>
this._buff[this._buffLen++] = 0x80;
hash.ts
258
>
this._buff.subarray(this._buffLen).fill(0);
259
>
260
>
if (this._buffLen > 56) {
261
this._step();
262
this._buff.fill(0);
263
}
265
>
// this will fit because the mantissa can cover up to 52 bits
266
>
const ml = 8 * this._totalLen;
267
>
268
>
this._buffDV.setUint32(56, Math.floor(ml / 4294967296), false);
269
>
this._buffDV.setUint32(60, ml % 4294967296, false);
270
>
271
>
this._step();
272
>
}
273
274
private _step(): void {
275
>
const bigBlock32 = StringSHA1._bigBlock32;
hash.ts
276
>
const data = this._buffDV;
277
>
278
>
for (let j = 0; j < 64 /* 16*4 */; j += 4) {
279
>
bigBlock32.setUint32(j, data.getUint32(j, false), false);
280
>
}
281
>
282
>
for (let j = 64; j < 320 /* 80*4 */; j += 4) {
283
>
bigBlock32.setUint32(j, leftRotate((bigBlock32.getUint32(j - 12, false) ^ bigBlock32.getUint32(j - 32, false) ^ bigBlock32.getUint32(j - 56, false) ^ bigBlock32.getUint32(j - 64, false)), 1), false);
284
>
}
285
>
286
>
let a = this._h0;
287
>
let b = this._h1;
288
>
let c = this._h2;
289
>
let d = this._h3;
290
>
let e = this._h4;
291
>
292
>
let f: number, k: number;
293
>
let temp: number;
294
>
295
>
for (let j = 0; j < 80; j++) {
296
>
if (j < 20) {
297
>
f = (b & c) | ((~b) & d);
298
>
k = 0x5A827999;
299
>
} else if (j < 40) {
300
>
f = b ^ c ^ d;
301
>
k = 0x6ED9EBA1;
302
>
} else if (j < 60) {
303
>
f = (b & c) | (b & d) | (c & d);
304
>
k = 0x8F1BBCDC;
305
>
} else {
306
>
f = b ^ c ^ d;
307
>
k = 0xCA62C1D6;
308
>
}
309
>
310
>
temp = (leftRotate(a, 5) + f + e + k + bigBlock32.getUint32(j * 4, false)) & 0xffffffff;
311
>
e = d;
312
>
d = c;
313
>
c = leftRotate(b, 30);
314
>
b = a;
315
>
a = temp;
316
>
}
317
>
318
>
this._h0 = (this._h0 + a) & 0xffffffff;
319
>
this._h1 = (this._h1 + b) & 0xffffffff;
320
>
this._h2 = (this._h2 + c) & 0xffffffff;
321
>
this._h3 = (this._h3 + d) & 0xffffffff;
322
>
this._h4 = (this._h4 + e) & 0xffffffff;
323
>
}
324
}