Skip to content

Commit b3e2423

Browse files
Merge pull request #45 from crypto-browserify/dcousens-patch-2
hash: _update never returns anything
2 parents 5d6baa3 + 22adba6 commit b3e2423

File tree

13 files changed

+57
-91
lines changed

13 files changed

+57
-91
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests/

bin.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ function usage () {
2828

2929
if (!process.stdin.isTTY) {
3030
pipe(argv[0], process.stdin)
31-
3231
} else if (argv.length) {
3332
if (/--help|-h/.test(argv[0])) {
3433
usage()
35-
3634
} else {
3735
var filename = argv.pop()
3836
var algorithm = argv.pop()

hash.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
var Buffer = require('buffer/').Buffer
1+
var Buffer = require('safe-buffer').Buffer
22

33
// prototype class for hash functions
44
function Hash (blockSize, finalSize) {
55
this._block = new Buffer(blockSize)
66
this._finalSize = finalSize
77
this._blockSize = blockSize
88
this._len = 0
9-
this._s = 0
109
}
1110

1211
Hash.prototype.update = function (data, enc) {
@@ -15,56 +14,62 @@ Hash.prototype.update = function (data, enc) {
1514
data = new Buffer(data, enc)
1615
}
1716

18-
var l = this._len += data.length
19-
var s = this._s || 0
20-
var f = 0
21-
var buffer = this._block
17+
var block = this._block
18+
var blockSize = this._blockSize
19+
var length = data.length
20+
var accum = this._len
2221

23-
while (s < l) {
24-
var t = Math.min(data.length, f + this._blockSize - (s % this._blockSize))
25-
var ch = (t - f)
22+
for (var offset = 0; offset < length;) {
23+
var assigned = accum % blockSize
24+
var remainder = Math.min(length - offset, blockSize - assigned)
2625

27-
for (var i = 0; i < ch; i++) {
28-
buffer[(s % this._blockSize) + i] = data[i + f]
26+
for (var i = 0; i < remainder; i++) {
27+
block[assigned + i] = data[offset + i]
2928
}
3029

31-
s += ch
32-
f += ch
30+
accum += remainder
31+
offset += remainder
3332

34-
if ((s % this._blockSize) === 0) {
35-
this._update(buffer)
33+
if ((accum % blockSize) === 0) {
34+
this._update(block)
3635
}
3736
}
38-
this._s = s
3937

38+
this._len += length
4039
return this
4140
}
4241

4342
Hash.prototype.digest = function (enc) {
44-
// Suppose the length of the message M, in bits, is l
45-
var l = this._len * 8
43+
var rem = this._len % this._blockSize
4644

47-
// Append the bit 1 to the end of the message
48-
this._block[this._len % this._blockSize] = 0x80
45+
this._block[rem] = 0x80
4946

50-
// and then k zero bits, where k is the smallest non-negative solution to the equation (l + 1 + k) === finalSize mod blockSize
51-
this._block.fill(0, this._len % this._blockSize + 1)
47+
// zero (rem + 1) trailing bits, where (rem + 1) is the smallest
48+
// non-negative solution to the equation (length + 1 + (rem + 1)) === finalSize mod blockSize
49+
this._block.fill(0, rem + 1)
5250

53-
if (l % (this._blockSize * 8) >= this._finalSize * 8) {
51+
if (rem >= this._finalSize) {
5452
this._update(this._block)
5553
this._block.fill(0)
5654
}
5755

58-
// to this append the block which is equal to the number l written in binary
59-
if (l <= 0xffffffff) {
60-
this._block.writeUInt32BE(l, this._blockSize - 4)
56+
var bits = this._len * 8
57+
58+
// uint32
59+
if (bits <= 0xffffffff) {
60+
this._block.writeUInt32BE(bits, this._blockSize - 4)
61+
62+
// uint64
6163
} else {
62-
var ll = l & 0xffffffff
63-
this._block.writeUInt32BE((l - ll) / 0x100000000, this._blockSize - 8)
64-
this._block.writeUInt32BE(ll, this._blockSize - 4)
64+
var lowBits = bits & 0xffffffff
65+
var highBits = (bits - lowBits) / 0x100000000
66+
67+
this._block.writeUInt32BE(highBits, this._blockSize - 8)
68+
this._block.writeUInt32BE(lowBits, this._blockSize - 4)
6569
}
6670

67-
var hash = this._update(this._block) || this._hash()
71+
this._update(this._block)
72+
var hash = this._hash()
6873

6974
return enc ? hash.toString(enc) : hash
7075
}

hexpp.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"inherits": "^2.0.1",
12-
"buffer": "^2.3.4"
12+
"safe-buffer": "^5.0.1"
1313
},
1414
"devDependencies": {
1515
"buffer": "~2.3.2",

sha.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
var inherits = require('inherits')
1010
var Hash = require('./hash')
11-
var Buffer = require('buffer/').Buffer
11+
var Buffer = require('safe-buffer').Buffer
1212

1313
var K = [
1414
0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0

sha1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
var inherits = require('inherits')
1111
var Hash = require('./hash')
12-
var Buffer = require('buffer/').Buffer
12+
var Buffer = require('safe-buffer').Buffer
1313

1414
var K = [
1515
0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0

sha224.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
var inherits = require('inherits')
1010
var Sha256 = require('./sha256')
1111
var Hash = require('./hash')
12-
var Buffer = require('buffer/').Buffer
12+
var Buffer = require('safe-buffer').Buffer
1313

1414
var W = new Array(64)
1515

sha256.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
var inherits = require('inherits')
1010
var Hash = require('./hash')
11-
var Buffer = require('buffer/').Buffer
11+
var Buffer = require('safe-buffer').Buffer
1212

1313
var K = [
1414
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,

sha384.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var inherits = require('inherits')
22
var SHA512 = require('./sha512')
33
var Hash = require('./hash')
4-
var Buffer = require('buffer/').Buffer
4+
var Buffer = require('safe-buffer').Buffer
55

66
var W = new Array(160)
77

0 commit comments

Comments
 (0)