Skip to content

Commit 659e629

Browse files
committed
Merge pull request #34 from fanatid/feature/perf
Improve performace
2 parents d97d992 + 6b341b7 commit 659e629

File tree

6 files changed

+226
-245
lines changed

6 files changed

+226
-245
lines changed

sha.js

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
var inherits = require('inherits')
1010
var Hash = require('./hash')
1111

12+
var K = [
13+
0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
14+
]
15+
1216
var W = new Array(80)
1317

1418
function Sha () {
@@ -21,62 +25,52 @@ function Sha () {
2125
inherits(Sha, Hash)
2226

2327
Sha.prototype.init = function () {
24-
this._a = 0x67452301 | 0
25-
this._b = 0xefcdab89 | 0
26-
this._c = 0x98badcfe | 0
27-
this._d = 0x10325476 | 0
28-
this._e = 0xc3d2e1f0 | 0
28+
this._a = 0x67452301
29+
this._b = 0xefcdab89
30+
this._c = 0x98badcfe
31+
this._d = 0x10325476
32+
this._e = 0xc3d2e1f0
2933

3034
return this
3135
}
3236

33-
/*
34-
* Bitwise rotate a 32-bit number to the left.
35-
*/
36-
function rol (num, cnt) {
37-
return (num << cnt) | (num >>> (32 - cnt))
37+
function rotl5 (num) {
38+
return (num << 5) | (num >>> 27)
39+
}
40+
41+
function rotl30 (num) {
42+
return (num << 30) | (num >>> 2)
43+
}
44+
45+
function ft (s, b, c, d) {
46+
if (s === 0) return (b & c) | ((~b) & d)
47+
if (s === 2) return (b & c) | (b & d) | (c & d)
48+
return b ^ c ^ d
3849
}
3950

4051
Sha.prototype._update = function (M) {
4152
var W = this._w
4253

43-
var a = this._a
44-
var b = this._b
45-
var c = this._c
46-
var d = this._d
47-
var e = this._e
48-
49-
var j = 0
50-
var k
54+
var a = this._a | 0
55+
var b = this._b | 0
56+
var c = this._c | 0
57+
var d = this._d | 0
58+
var e = this._e | 0
5159

52-
/*
53-
* SHA-1 has a bitwise rotate left operation. But, SHA is not
54-
* function calcW() { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
55-
*/
56-
function calcW () { return W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16] }
57-
function loop (w, f) {
58-
W[j] = w
60+
for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
61+
for (; i < 80; ++i) W[i] = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]
5962

60-
var t = rol(a, 5) + f + e + w + k
63+
for (var j = 0; j < 80; ++j) {
64+
var s = ~~(j / 20)
65+
var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
6166

6267
e = d
6368
d = c
64-
c = rol(b, 30)
69+
c = rotl30(b)
6570
b = a
6671
a = t
67-
j++
6872
}
6973

70-
k = 1518500249
71-
while (j < 16) loop(M.readInt32BE(j * 4), (b & c) | ((~b) & d))
72-
while (j < 20) loop(calcW(), (b & c) | ((~b) & d))
73-
k = 1859775393
74-
while (j < 40) loop(calcW(), b ^ c ^ d)
75-
k = -1894007588
76-
while (j < 60) loop(calcW(), (b & c) | (b & d) | (c & d))
77-
k = -899497514
78-
while (j < 80) loop(calcW(), b ^ c ^ d)
79-
8074
this._a = (a + this._a) | 0
8175
this._b = (b + this._b) | 0
8276
this._c = (c + this._c) | 0
@@ -97,4 +91,3 @@ Sha.prototype._hash = function () {
9791
}
9892

9993
module.exports = Sha
100-

sha1.js

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
var inherits = require('inherits')
1111
var Hash = require('./hash')
1212

13+
var K = [
14+
0x5a827999, 0x6ed9eba1, 0x8f1bbcdc | 0, 0xca62c1d6 | 0
15+
]
16+
1317
var W = new Array(80)
1418

1519
function Sha1 () {
@@ -22,58 +26,56 @@ function Sha1 () {
2226
inherits(Sha1, Hash)
2327

2428
Sha1.prototype.init = function () {
25-
this._a = 0x67452301 | 0
26-
this._b = 0xefcdab89 | 0
27-
this._c = 0x98badcfe | 0
28-
this._d = 0x10325476 | 0
29-
this._e = 0xc3d2e1f0 | 0
29+
this._a = 0x67452301
30+
this._b = 0xefcdab89
31+
this._c = 0x98badcfe
32+
this._d = 0x10325476
33+
this._e = 0xc3d2e1f0
3034

3135
return this
3236
}
3337

34-
/*
35-
* Bitwise rotate a 32-bit number to the left.
36-
*/
37-
function rol (num, cnt) {
38-
return (num << cnt) | (num >>> (32 - cnt))
38+
function rotl1 (num) {
39+
return (num << 1) | (num >>> 31)
40+
}
41+
42+
function rotl5 (num) {
43+
return (num << 5) | (num >>> 27)
44+
}
45+
46+
function rotl30 (num) {
47+
return (num << 30) | (num >>> 2)
48+
}
49+
50+
function ft (s, b, c, d) {
51+
if (s === 0) return (b & c) | ((~b) & d)
52+
if (s === 2) return (b & c) | (b & d) | (c & d)
53+
return b ^ c ^ d
3954
}
4055

4156
Sha1.prototype._update = function (M) {
4257
var W = this._w
4358

44-
var a = this._a
45-
var b = this._b
46-
var c = this._c
47-
var d = this._d
48-
var e = this._e
59+
var a = this._a | 0
60+
var b = this._b | 0
61+
var c = this._c | 0
62+
var d = this._d | 0
63+
var e = this._e | 0
4964

50-
var j = 0
51-
var k
65+
for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
66+
for (; i < 80; ++i) W[i] = rotl1(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16])
5267

53-
function calcW () { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
54-
function loop (w, f) {
55-
W[j] = w
56-
57-
var t = rol(a, 5) + f + e + w + k
68+
for (var j = 0; j < 80; ++j) {
69+
var s = ~~(j / 20)
70+
var t = (rotl5(a) + ft(s, b, c, d) + e + W[j] + K[s]) | 0
5871

5972
e = d
6073
d = c
61-
c = rol(b, 30)
74+
c = rotl30(b)
6275
b = a
6376
a = t
64-
j++
6577
}
6678

67-
k = 1518500249
68-
while (j < 16) loop(M.readInt32BE(j * 4), (b & c) | ((~b) & d))
69-
while (j < 20) loop(calcW(), (b & c) | ((~b) & d))
70-
k = 1859775393
71-
while (j < 40) loop(calcW(), b ^ c ^ d)
72-
k = -1894007588
73-
while (j < 60) loop(calcW(), (b & c) | (b & d) | (c & d))
74-
k = -899497514
75-
while (j < 80) loop(calcW(), b ^ c ^ d)
76-
7779
this._a = (a + this._a) | 0
7880
this._b = (b + this._b) | 0
7981
this._c = (c + this._c) | 0

sha224.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ function Sha224 () {
2323
inherits(Sha224, Sha256)
2424

2525
Sha224.prototype.init = function () {
26-
this._a = 0xc1059ed8 | 0
27-
this._b = 0x367cd507 | 0
28-
this._c = 0x3070dd17 | 0
29-
this._d = 0xf70e5939 | 0
30-
this._e = 0xffc00b31 | 0
31-
this._f = 0x68581511 | 0
32-
this._g = 0x64f98fa7 | 0
33-
this._h = 0xbefa4fa4 | 0
26+
this._a = 0xc1059ed8
27+
this._b = 0x367cd507
28+
this._c = 0x3070dd17
29+
this._d = 0xf70e5939
30+
this._e = 0xffc00b31
31+
this._f = 0x68581511
32+
this._g = 0x64f98fa7
33+
this._h = 0xbefa4fa4
3434

3535
return this
3636
}

sha256.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,39 @@ function Sha256 () {
4141
inherits(Sha256, Hash)
4242

4343
Sha256.prototype.init = function () {
44-
this._a = 0x6a09e667 | 0
45-
this._b = 0xbb67ae85 | 0
46-
this._c = 0x3c6ef372 | 0
47-
this._d = 0xa54ff53a | 0
48-
this._e = 0x510e527f | 0
49-
this._f = 0x9b05688c | 0
50-
this._g = 0x1f83d9ab | 0
51-
this._h = 0x5be0cd19 | 0
44+
this._a = 0x6a09e667
45+
this._b = 0xbb67ae85
46+
this._c = 0x3c6ef372
47+
this._d = 0xa54ff53a
48+
this._e = 0x510e527f
49+
this._f = 0x9b05688c
50+
this._g = 0x1f83d9ab
51+
this._h = 0x5be0cd19
5252

5353
return this
5454
}
5555

56-
function Ch (x, y, z) {
56+
function ch (x, y, z) {
5757
return z ^ (x & (y ^ z))
5858
}
5959

60-
function Maj (x, y, z) {
60+
function maj (x, y, z) {
6161
return (x & y) | (z & (x | y))
6262
}
6363

64-
function Sigma0 (x) {
64+
function sigma0 (x) {
6565
return (x >>> 2 | x << 30) ^ (x >>> 13 | x << 19) ^ (x >>> 22 | x << 10)
6666
}
6767

68-
function Sigma1 (x) {
68+
function sigma1 (x) {
6969
return (x >>> 6 | x << 26) ^ (x >>> 11 | x << 21) ^ (x >>> 25 | x << 7)
7070
}
7171

72-
function Gamma0 (x) {
72+
function gamma0 (x) {
7373
return (x >>> 7 | x << 25) ^ (x >>> 18 | x << 14) ^ (x >>> 3)
7474
}
7575

76-
function Gamma1 (x) {
76+
function gamma1 (x) {
7777
return (x >>> 17 | x << 15) ^ (x >>> 19 | x << 13) ^ (x >>> 10)
7878
}
7979

@@ -89,30 +89,23 @@ Sha256.prototype._update = function (M) {
8989
var g = this._g | 0
9090
var h = this._h | 0
9191

92-
var j = 0
92+
for (var i = 0; i < 16; ++i) W[i] = M.readInt32BE(i * 4)
93+
for (; i < 64; ++i) W[i] = (gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16]) | 0
9394

94-
function calcW () { return Gamma1(W[j - 2]) + W[j - 7] + Gamma0(W[j - 15]) + W[j - 16] }
95-
function loop (w) {
96-
W[j] = w
97-
98-
var T1 = h + Sigma1(e) + Ch(e, f, g) + K[j] + w
99-
var T2 = Sigma0(a) + Maj(a, b, c)
95+
for (var j = 0; j < 64; ++j) {
96+
var T1 = (h + sigma1(e) + ch(e, f, g) + K[j] + W[j]) | 0
97+
var T2 = (sigma0(a) + maj(a, b, c)) | 0
10098

10199
h = g
102100
g = f
103101
f = e
104-
e = d + T1
102+
e = (d + T1) | 0
105103
d = c
106104
c = b
107105
b = a
108-
a = T1 + T2
109-
110-
j++
106+
a = (T1 + T2) | 0
111107
}
112108

113-
while (j < 16) loop(M.readInt32BE(j * 4))
114-
while (j < 64) loop(calcW())
115-
116109
this._a = (a + this._a) | 0
117110
this._b = (b + this._b) | 0
118111
this._c = (c + this._c) | 0

sha384.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ function Sha384 () {
1414
inherits(Sha384, SHA512)
1515

1616
Sha384.prototype.init = function () {
17-
this._a = 0xcbbb9d5d | 0
18-
this._b = 0x629a292a | 0
19-
this._c = 0x9159015a | 0
20-
this._d = 0x152fecd8 | 0
21-
this._e = 0x67332667 | 0
22-
this._f = 0x8eb44a87 | 0
23-
this._g = 0xdb0c2e0d | 0
24-
this._h = 0x47b5481d | 0
25-
26-
this._al = 0xc1059ed8 | 0
27-
this._bl = 0x367cd507 | 0
28-
this._cl = 0x3070dd17 | 0
29-
this._dl = 0xf70e5939 | 0
30-
this._el = 0xffc00b31 | 0
31-
this._fl = 0x68581511 | 0
32-
this._gl = 0x64f98fa7 | 0
33-
this._hl = 0xbefa4fa4 | 0
17+
this._ah = 0xcbbb9d5d
18+
this._bh = 0x629a292a
19+
this._ch = 0x9159015a
20+
this._dh = 0x152fecd8
21+
this._eh = 0x67332667
22+
this._fh = 0x8eb44a87
23+
this._gh = 0xdb0c2e0d
24+
this._hh = 0x47b5481d
25+
26+
this._al = 0xc1059ed8
27+
this._bl = 0x367cd507
28+
this._cl = 0x3070dd17
29+
this._dl = 0xf70e5939
30+
this._el = 0xffc00b31
31+
this._fl = 0x68581511
32+
this._gl = 0x64f98fa7
33+
this._hl = 0xbefa4fa4
3434

3535
return this
3636
}
@@ -43,12 +43,12 @@ Sha384.prototype._hash = function () {
4343
H.writeInt32BE(l, offset + 4)
4444
}
4545

46-
writeInt64BE(this._a, this._al, 0)
47-
writeInt64BE(this._b, this._bl, 8)
48-
writeInt64BE(this._c, this._cl, 16)
49-
writeInt64BE(this._d, this._dl, 24)
50-
writeInt64BE(this._e, this._el, 32)
51-
writeInt64BE(this._f, this._fl, 40)
46+
writeInt64BE(this._ah, this._al, 0)
47+
writeInt64BE(this._bh, this._bl, 8)
48+
writeInt64BE(this._ch, this._cl, 16)
49+
writeInt64BE(this._dh, this._dl, 24)
50+
writeInt64BE(this._eh, this._el, 32)
51+
writeInt64BE(this._fh, this._fl, 40)
5252

5353
return H
5454
}

0 commit comments

Comments
 (0)