Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80644 views
1
var hash = require('../hash');
2
var utils = hash.utils;
3
var assert = utils.assert;
4
5
function BlockHash() {
6
this.pending = null;
7
this.pendingTotal = 0;
8
this.blockSize = this.constructor.blockSize;
9
this.outSize = this.constructor.outSize;
10
this.hmacStrength = this.constructor.hmacStrength;
11
this.padLength = this.constructor.padLength / 8;
12
this.endian = 'big';
13
14
this._delta8 = this.blockSize / 8;
15
this._delta32 = this.blockSize / 32;
16
}
17
exports.BlockHash = BlockHash;
18
19
BlockHash.prototype.update = function update(msg, enc) {
20
// Convert message to array, pad it, and join into 32bit blocks
21
msg = utils.toArray(msg, enc);
22
if (!this.pending)
23
this.pending = msg;
24
else
25
this.pending = this.pending.concat(msg);
26
this.pendingTotal += msg.length;
27
28
// Enough data, try updating
29
if (this.pending.length >= this._delta8) {
30
msg = this.pending;
31
32
// Process pending data in blocks
33
var r = msg.length % this._delta8;
34
this.pending = msg.slice(msg.length - r, msg.length);
35
if (this.pending.length === 0)
36
this.pending = null;
37
38
msg = utils.join32(msg, 0, msg.length - r, this.endian);
39
for (var i = 0; i < msg.length; i += this._delta32)
40
this._update(msg, i, i + this._delta32);
41
}
42
43
return this;
44
};
45
46
BlockHash.prototype.digest = function digest(enc) {
47
this.update(this._pad());
48
assert(this.pending === null);
49
50
return this._digest(enc);
51
};
52
53
BlockHash.prototype._pad = function pad() {
54
var len = this.pendingTotal;
55
var bytes = this._delta8;
56
var k = bytes - ((len + this.padLength) % bytes);
57
var res = new Array(k + this.padLength);
58
res[0] = 0x80;
59
for (var i = 1; i < k; i++)
60
res[i] = 0;
61
62
// Append length
63
len <<= 3;
64
if (this.endian === 'big') {
65
for (var t = 8; t < this.padLength; t++)
66
res[i++] = 0;
67
68
res[i++] = 0;
69
res[i++] = 0;
70
res[i++] = 0;
71
res[i++] = 0;
72
res[i++] = (len >>> 24) & 0xff;
73
res[i++] = (len >>> 16) & 0xff;
74
res[i++] = (len >>> 8) & 0xff;
75
res[i++] = len & 0xff;
76
} else {
77
res[i++] = len & 0xff;
78
res[i++] = (len >>> 8) & 0xff;
79
res[i++] = (len >>> 16) & 0xff;
80
res[i++] = (len >>> 24) & 0xff;
81
res[i++] = 0;
82
res[i++] = 0;
83
res[i++] = 0;
84
res[i++] = 0;
85
86
for (var t = 8; t < this.padLength; t++)
87
res[i++] = 0;
88
}
89
90
return res;
91
};
92
93