9
9
var inherits = require ( 'inherits' )
10
10
var Hash = require ( './hash' )
11
11
12
+ var K = [
13
+ 0x5a827999 , 0x6ed9eba1 , 0x8f1bbcdc | 0 , 0xca62c1d6 | 0
14
+ ]
15
+
12
16
var W = new Array ( 80 )
13
17
14
18
function Sha ( ) {
@@ -21,62 +25,52 @@ function Sha () {
21
25
inherits ( Sha , Hash )
22
26
23
27
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
29
33
30
34
return this
31
35
}
32
36
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
38
49
}
39
50
40
51
Sha . prototype . _update = function ( M ) {
41
52
var W = this . _w
42
53
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
51
59
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 ]
59
62
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
61
66
62
67
e = d
63
68
d = c
64
- c = rol ( b , 30 )
69
+ c = rotl30 ( b )
65
70
b = a
66
71
a = t
67
- j ++
68
72
}
69
73
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
-
80
74
this . _a = ( a + this . _a ) | 0
81
75
this . _b = ( b + this . _b ) | 0
82
76
this . _c = ( c + this . _c ) | 0
@@ -97,4 +91,3 @@ Sha.prototype._hash = function () {
97
91
}
98
92
99
93
module . exports = Sha
100
-
0 commit comments