-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.go
154 lines (121 loc) · 3.16 KB
/
digest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package has160
import (
"hash"
)
// The size of a HAS-160 checksum in bytes.
const Size = 20
// The blocksize of HAS-160 in bytes.
const BlockSize = 64
type digest struct {
s [5]uint32
x [BlockSize]byte
nx int
len uint64
}
// New returns a new hash.Hash computing the HAS-160 checksum.
func New() hash.Hash {
c := new(digest)
c.Reset()
return c
}
func (this *digest) Size() int {
return Size
}
func (this *digest) BlockSize() int {
return BlockSize
}
func (this *digest) Reset() {
this.nx = 0
this.len = 0
this.s = [5]uint32{}
this.x = [BlockSize]byte{}
copy(this.s[:], initH[:])
}
func (this *digest) Write(p []byte) (nn int, err error) {
nn = len(p)
plen := len(p)
var limit = BlockSize
for this.nx + plen >= limit {
xx := limit - this.nx
copy(this.x[this.nx:], p)
this.stepBlock(this.x[:])
plen -= xx
this.len += uint64(xx)
p = p[xx:]
this.nx = 0
}
copy(this.x[this.nx:], p)
this.nx += plen
this.len += uint64(plen)
return
}
func (this *digest) Sum(p []byte) []byte {
d0 := *this
hash := d0.checkSum()
return append(p, hash[:]...)
}
func (this *digest) checkSum() (hash [20]byte) {
this.x[this.nx] = 0x80
this.nx++
zeros := make([]byte, BlockSize)
if this.nx > BlockSize - 8 {
copy(this.x[this.nx:], zeros)
this.stepBlock(this.x[:])
this.nx = 0
}
copy(this.x[this.nx:], zeros)
PUTU64(this.x[BlockSize-8:], this.len*8)
this.stepBlock(this.x[:])
sum := uint32sToBytes(this.s[:])
copy(hash[:], sum)
return
}
func (this *digest) stepBlock(block []byte) {
x := make([]uint32, 20)
blocks := bytesToUint32s(block)
copy(x, blocks)
var H [5]uint32
copy(H[:], this.s[:])
h := H[:]
// 00 ~ 19
// Round 1
x[16] = x[0] ^ x[1] ^ x[2] ^ x[3]
x[17] = x[4] ^ x[5] ^ x[6] ^ x[7]
x[18] = x[8] ^ x[9] ^ x[10] ^ x[11]
x[19] = x[12] ^ x[13] ^ x[14] ^ x[15]
for i := 0; i < 20; i++ {
f1(h[:], 4-(4+i)%5, 4-(3+i)%5, 4-(2+i)%5, 4-(1+i)%5, 4-i%5, x, i) // j = 00 + i
}
// 20 ~ 39
// Round 2
x[16] = x[3] ^ x[6] ^ x[9] ^ x[12]
x[17] = x[2] ^ x[5] ^ x[8] ^ x[15]
x[18] = x[1] ^ x[4] ^ x[11] ^ x[14]
x[19] = x[0] ^ x[7] ^ x[10] ^ x[13]
for i := 0; i < 20; i++ {
f2(h[:], 4-(4+i)%5, 4-(3+i)%5, 4-(2+i)%5, 4-(1+i)%5, 4-i%5, x, i) // j = 20 + i
}
// 40 ~ 59
// Round 3
x[16] = x[5] ^ x[7] ^ x[12] ^ x[14]
x[17] = x[0] ^ x[2] ^ x[9] ^ x[11]
x[18] = x[4] ^ x[6] ^ x[13] ^ x[15]
x[19] = x[1] ^ x[3] ^ x[8] ^ x[10]
for i := 0; i < 20; i++ {
f3(h[:], 4-(4+i)%5, 4-(3+i)%5, 4-(2+i)%5, 4-(1+i)%5, 4-i%5, x, i) // j = 40 + i
}
// 60 ~ 79
// Round 4
x[16] = x[2] ^ x[7] ^ x[8] ^ x[13]
x[17] = x[3] ^ x[4] ^ x[9] ^ x[14]
x[18] = x[0] ^ x[5] ^ x[10] ^ x[15]
x[19] = x[1] ^ x[6] ^ x[11] ^ x[12]
for i := 0; i < 20; i++ {
f4(h[:], 4-(4+i)%5, 4-(3+i)%5, 4-(2+i)%5, 4-(1+i)%5, 4-i%5, x, i) // j = 60 + i
}
this.s[0] += h[0]
this.s[1] += h[1]
this.s[2] += h[2]
this.s[3] += h[3]
this.s[4] += h[4]
}