Skip to content

Commit 74f5fc4

Browse files
committed
sha*: adhere to standard
1 parent 5657c76 commit 74f5fc4

File tree

6 files changed

+142
-143
lines changed

6 files changed

+142
-143
lines changed

sha.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var Hash = require('./hash')
1111

1212
var W = new Array(80)
1313

14-
function Sha() {
14+
function Sha () {
1515
this.init()
1616
this._w = W
1717

@@ -21,20 +21,20 @@ function Sha() {
2121
inherits(Sha, Hash)
2222

2323
Sha.prototype.init = function () {
24-
this._a = 0x67452301
25-
this._b = 0xefcdab89
26-
this._c = 0x98badcfe
27-
this._d = 0x10325476
28-
this._e = 0xc3d2e1f0
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
2929

3030
return this
3131
}
3232

3333
/*
3434
* Bitwise rotate a 32-bit number to the left.
3535
*/
36-
function rol(num, cnt) {
37-
return (num << cnt) | (num >>> (32 - cnt));
36+
function rol (num, cnt) {
37+
return (num << cnt) | (num >>> (32 - cnt))
3838
}
3939

4040
Sha.prototype._update = function (M) {
@@ -52,8 +52,8 @@ Sha.prototype._update = function (M) {
5252
* SHA-1 has a bitwise rotate left operation. But, SHA is not
5353
* function calcW() { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
5454
*/
55-
function calcW() { return W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16] }
56-
function loop(w, f) {
55+
function calcW () { return W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16] }
56+
function loop (w, f) {
5757
W[j] = w
5858

5959
var t = rol(a, 5) + f + e + w + k
@@ -86,11 +86,11 @@ Sha.prototype._update = function (M) {
8686
Sha.prototype._hash = function () {
8787
var H = new Buffer(20)
8888

89-
H.writeInt32BE(this._a|0, 0)
90-
H.writeInt32BE(this._b|0, 4)
91-
H.writeInt32BE(this._c|0, 8)
92-
H.writeInt32BE(this._d|0, 12)
93-
H.writeInt32BE(this._e|0, 16)
89+
H.writeInt32BE(this._a | 0, 0)
90+
H.writeInt32BE(this._b | 0, 4)
91+
H.writeInt32BE(this._c | 0, 8)
92+
H.writeInt32BE(this._d | 0, 12)
93+
H.writeInt32BE(this._e | 0, 16)
9494

9595
return H
9696
}

sha1.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Hash = require('./hash')
1212

1313
var W = new Array(80)
1414

15-
function Sha1() {
15+
function Sha1 () {
1616
this.init()
1717
this._w = W
1818

@@ -22,20 +22,20 @@ function Sha1() {
2222
inherits(Sha1, Hash)
2323

2424
Sha1.prototype.init = function () {
25-
this._a = 0x67452301
26-
this._b = 0xefcdab89
27-
this._c = 0x98badcfe
28-
this._d = 0x10325476
29-
this._e = 0xc3d2e1f0
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
3030

3131
return this
3232
}
3333

3434
/*
3535
* Bitwise rotate a 32-bit number to the left.
3636
*/
37-
function rol(num, cnt) {
38-
return (num << cnt) | (num >>> (32 - cnt));
37+
function rol (num, cnt) {
38+
return (num << cnt) | (num >>> (32 - cnt))
3939
}
4040

4141
Sha1.prototype._update = function (M) {
@@ -49,8 +49,8 @@ Sha1.prototype._update = function (M) {
4949

5050
var j = 0, k
5151

52-
function calcW() { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
53-
function loop(w, f) {
52+
function calcW () { return rol(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1) }
53+
function loop (w, f) {
5454
W[j] = w
5555

5656
var t = rol(a, 5) + f + e + w + k
@@ -83,14 +83,13 @@ Sha1.prototype._update = function (M) {
8383
Sha1.prototype._hash = function () {
8484
var H = new Buffer(20)
8585

86-
H.writeInt32BE(this._a|0, 0)
87-
H.writeInt32BE(this._b|0, 4)
88-
H.writeInt32BE(this._c|0, 8)
89-
H.writeInt32BE(this._d|0, 12)
90-
H.writeInt32BE(this._e|0, 16)
86+
H.writeInt32BE(this._a | 0, 0)
87+
H.writeInt32BE(this._b | 0, 4)
88+
H.writeInt32BE(this._c | 0, 8)
89+
H.writeInt32BE(this._d | 0, 12)
90+
H.writeInt32BE(this._e | 0, 16)
9191

9292
return H
9393
}
9494

9595
module.exports = Sha1
96-

sha224.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,40 @@
77
*/
88

99
var inherits = require('inherits')
10-
var SHA256 = require('./sha256')
10+
var Sha256 = require('./sha256')
1111
var Hash = require('./hash')
1212

1313
var W = new Array(64)
1414

15-
function Sha224() {
15+
function Sha224 () {
1616
this.init()
1717

1818
this._w = W // new Array(64)
1919

2020
Hash.call(this, 64, 56)
2121
}
2222

23-
inherits(Sha224, SHA256)
23+
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 | 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
3434

3535
return this
3636
}
3737

3838
Sha224.prototype._hash = function () {
3939
var H = new Buffer(28)
4040

41-
H.writeInt32BE(this._a, 0)
42-
H.writeInt32BE(this._b, 4)
43-
H.writeInt32BE(this._c, 8)
41+
H.writeInt32BE(this._a, 0)
42+
H.writeInt32BE(this._b, 4)
43+
H.writeInt32BE(this._c, 8)
4444
H.writeInt32BE(this._d, 12)
4545
H.writeInt32BE(this._e, 16)
4646
H.writeInt32BE(this._f, 20)

sha256.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var K = [
3030

3131
var W = new Array(64)
3232

33-
function Sha256() {
33+
function Sha256 () {
3434
this.init()
3535

3636
this._w = W // new Array(64)
@@ -41,51 +41,51 @@ 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 | 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
5252

5353
return this
5454
}
5555

5656
function S (X, n) {
57-
return (X >>> n) | (X << (32 - n));
57+
return (X >>> n) | (X << (32 - n))
5858
}
5959

6060
function R (X, n) {
61-
return (X >>> n);
61+
return (X >>> n)
6262
}
6363

6464
function Ch (x, y, z) {
65-
return ((x & y) ^ ((~x) & z));
65+
return ((x & y) ^ ((~x) & z))
6666
}
6767

6868
function Maj (x, y, z) {
69-
return ((x & y) ^ (x & z) ^ (y & z));
69+
return ((x & y) ^ (x & z) ^ (y & z))
7070
}
7171

7272
function Sigma0256 (x) {
73-
return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
73+
return (S(x, 2) ^ S(x, 13) ^ S(x, 22))
7474
}
7575

7676
function Sigma1256 (x) {
77-
return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
77+
return (S(x, 6) ^ S(x, 11) ^ S(x, 25))
7878
}
7979

8080
function Gamma0256 (x) {
81-
return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
81+
return (S(x, 7) ^ S(x, 18) ^ R(x, 3))
8282
}
8383

8484
function Gamma1256 (x) {
85-
return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
85+
return (S(x, 17) ^ S(x, 19) ^ R(x, 10))
8686
}
8787

88-
Sha256.prototype._update = function(M) {
88+
Sha256.prototype._update = function (M) {
8989
var W = this._w
9090

9191
var a = this._a | 0
@@ -99,21 +99,21 @@ Sha256.prototype._update = function(M) {
9999

100100
var j = 0
101101

102-
function calcW() { return Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16] }
103-
function loop(w) {
102+
function calcW () { return Gamma1256(W[j - 2]) + W[j - 7] + Gamma0256(W[j - 15]) + W[j - 16] }
103+
function loop (w) {
104104
W[j] = w
105105

106106
var T1 = h + Sigma1256(e) + Ch(e, f, g) + K[j] + w
107-
var T2 = Sigma0256(a) + Maj(a, b, c);
107+
var T2 = Sigma0256(a) + Maj(a, b, c)
108108

109-
h = g;
110-
g = f;
111-
f = e;
112-
e = d + T1;
113-
d = c;
114-
c = b;
115-
b = a;
116-
a = T1 + T2;
109+
h = g
110+
g = f
111+
f = e
112+
e = d + T1
113+
d = c
114+
c = b
115+
b = a
116+
a = T1 + T2
117117

118118
j++
119119
}
@@ -129,14 +129,14 @@ Sha256.prototype._update = function(M) {
129129
this._f = (f + this._f) | 0
130130
this._g = (g + this._g) | 0
131131
this._h = (h + this._h) | 0
132-
};
132+
}
133133

134134
Sha256.prototype._hash = function () {
135135
var H = new Buffer(32)
136136

137-
H.writeInt32BE(this._a, 0)
138-
H.writeInt32BE(this._b, 4)
139-
H.writeInt32BE(this._c, 8)
137+
H.writeInt32BE(this._a, 0)
138+
H.writeInt32BE(this._b, 4)
139+
H.writeInt32BE(this._c, 8)
140140
H.writeInt32BE(this._d, 12)
141141
H.writeInt32BE(this._e, 16)
142142
H.writeInt32BE(this._f, 20)

sha384.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
var inherits = require('inherits')
2-
var SHA512 = require('./sha512');
2+
var SHA512 = require('./sha512')
33
var Hash = require('./hash')
44

55
var W = new Array(160)
66

7-
function Sha384() {
7+
function Sha384 () {
88
this.init()
99
this._w = W
1010

@@ -14,31 +14,31 @@ 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._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
3434

3535
return this
3636
}
3737

3838
Sha384.prototype._hash = function () {
3939
var H = new Buffer(48)
4040

41-
function writeInt64BE(h, l, offset) {
41+
function writeInt64BE (h, l, offset) {
4242
H.writeInt32BE(h, offset)
4343
H.writeInt32BE(l, offset + 4)
4444
}

0 commit comments

Comments
 (0)