Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // SHA1 block step. | 5 // SHA1 block step. |
6 // In its own file so that a faster assembly or C version | 6 // In its own file so that a faster assembly or C version |
7 // can be substituted easily. | 7 // can be substituted easily. |
8 | 8 |
9 package sha1 | 9 package sha1 |
10 | 10 |
11 const ( | 11 const ( |
12 _K0 = 0x5A827999 | 12 _K0 = 0x5A827999 |
13 _K1 = 0x6ED9EBA1 | 13 _K1 = 0x6ED9EBA1 |
14 _K2 = 0x8F1BBCDC | 14 _K2 = 0x8F1BBCDC |
15 _K3 = 0xCA62C1D6 | 15 _K3 = 0xCA62C1D6 |
16 ) | 16 ) |
17 | 17 |
18 func block(dig *digest, p []byte) { | 18 func block(dig *digest, p []byte) { |
19 » var w [80]uint32 | 19 » var w [16]uint32 |
20 | 20 |
21 h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] | 21 h0, h1, h2, h3, h4 := dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] |
22 for len(p) >= chunk { | 22 for len(p) >= chunk { |
23 // Can interlace the computation of w with the | 23 // Can interlace the computation of w with the |
24 // rounds below if needed for speed. | 24 // rounds below if needed for speed. |
25 for i := 0; i < 16; i++ { | 25 for i := 0; i < 16; i++ { |
26 j := i * 4 | 26 j := i * 4 |
27 w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[ j+2])<<8 | uint32(p[j+3]) | 27 w[i] = uint32(p[j])<<24 | uint32(p[j+1])<<16 | uint32(p[ j+2])<<8 | uint32(p[j+3]) |
28 } | 28 } |
29 for i := 16; i < 80; i++ { | |
30 tmp := w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] | |
31 w[i] = tmp<<1 | tmp>>(32-1) | |
32 } | |
33 | 29 |
34 a, b, c, d, e := h0, h1, h2, h3, h4 | 30 a, b, c, d, e := h0, h1, h2, h3, h4 |
35 | 31 |
36 // Each of the four 20-iteration rounds | 32 // Each of the four 20-iteration rounds |
37 // differs only in the computation of f and | 33 // differs only in the computation of f and |
38 // the choice of K (_K0, _K1, etc). | 34 // the choice of K (_K0, _K1, etc). |
39 » » for i := 0; i < 20; i++ { | 35 » » for i := 0; i < 16; i++ { |
dfc
2012/11/06 04:06:47
lifting i outside the for loop, then not initalisi
| |
40 f := b&c | (^b)&d | 36 f := b&c | (^b)&d |
41 a5 := a<<5 | a>>(32-5) | 37 a5 := a<<5 | a>>(32-5) |
42 b30 := b<<30 | b>>(32-30) | 38 b30 := b<<30 | b>>(32-30) |
43 t := a5 + f + e + w[i] + _K0 | 39 t := a5 + f + e + w[i] + _K0 |
rsc
2012/11/06 18:52:06
i&0xf will get you something here.
notcarl
2012/11/06 19:02:34
I am not sure I understand. Since the loop only g
| |
44 a, b, c, d, e = t, a, b30, c, d | 40 a, b, c, d, e = t, a, b30, c, d |
45 } | 41 } |
42 for i := 16; i < 20; i++ { | |
dfc
2012/11/06 04:06:47
for ; i < 20; i++ { .. }
and so forth
rsc
2012/11/06 18:52:06
FWIW this is unlikely to matter.
notcarl
2012/11/06 19:02:34
I was hoping that maybe the compiler saw this. I
| |
43 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] | |
44 w[i&0xf] = tmp<<1 | tmp>>(32-1) | |
45 | |
46 f := b&c | (^b)&d | |
47 a5 := a<<5 | a>>(32-5) | |
48 b30 := b<<30 | b>>(32-30) | |
49 t := a5 + f + e + w[i&0xf] + _K0 | |
50 a, b, c, d, e = t, a, b30, c, d | |
51 } | |
46 for i := 20; i < 40; i++ { | 52 for i := 20; i < 40; i++ { |
53 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] | |
54 w[i&0xf] = tmp<<1 | tmp>>(32-1) | |
47 f := b ^ c ^ d | 55 f := b ^ c ^ d |
48 a5 := a<<5 | a>>(32-5) | 56 a5 := a<<5 | a>>(32-5) |
49 b30 := b<<30 | b>>(32-30) | 57 b30 := b<<30 | b>>(32-30) |
50 » » » t := a5 + f + e + w[i] + _K1 | 58 » » » t := a5 + f + e + w[i&0xf] + _K1 |
51 a, b, c, d, e = t, a, b30, c, d | 59 a, b, c, d, e = t, a, b30, c, d |
52 } | 60 } |
53 for i := 40; i < 60; i++ { | 61 for i := 40; i < 60; i++ { |
54 » » » f := b&c | b&d | c&d | 62 » » » tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] |
63 » » » w[i&0xf] = tmp<<1 | tmp>>(32-1) | |
64 » » » f := ((b | c) & d) | (b & c) | |
65 | |
55 a5 := a<<5 | a>>(32-5) | 66 a5 := a<<5 | a>>(32-5) |
56 b30 := b<<30 | b>>(32-30) | 67 b30 := b<<30 | b>>(32-30) |
57 » » » t := a5 + f + e + w[i] + _K2 | 68 » » » t := a5 + f + e + w[i&0xf] + _K2 |
58 a, b, c, d, e = t, a, b30, c, d | 69 a, b, c, d, e = t, a, b30, c, d |
59 } | 70 } |
60 for i := 60; i < 80; i++ { | 71 for i := 60; i < 80; i++ { |
72 tmp := w[(i-3)&0xf] ^ w[(i-8)&0xf] ^ w[(i-14)&0xf] ^ w[( i)&0xf] | |
73 w[i&0xf] = tmp<<1 | tmp>>(32-1) | |
61 f := b ^ c ^ d | 74 f := b ^ c ^ d |
62 a5 := a<<5 | a>>(32-5) | 75 a5 := a<<5 | a>>(32-5) |
63 b30 := b<<30 | b>>(32-30) | 76 b30 := b<<30 | b>>(32-30) |
64 » » » t := a5 + f + e + w[i] + _K3 | 77 » » » t := a5 + f + e + w[i&0xf] + _K3 |
65 a, b, c, d, e = t, a, b30, c, d | 78 a, b, c, d, e = t, a, b30, c, d |
66 } | 79 } |
67 | 80 |
68 h0 += a | 81 h0 += a |
69 h1 += b | 82 h1 += b |
70 h2 += c | 83 h2 += c |
71 h3 += d | 84 h3 += d |
72 h4 += e | 85 h4 += e |
73 | 86 |
74 p = p[chunk:] | 87 p = p[chunk:] |
75 } | 88 } |
76 | 89 |
77 dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4 | 90 dig.h[0], dig.h[1], dig.h[2], dig.h[3], dig.h[4] = h0, h1, h2, h3, h4 |
78 } | 91 } |
OLD | NEW |