/
hashing.go
254 lines (204 loc) · 5.42 KB
/
hashing.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package mymath
// Copyright 2011-2015 ThePiachu. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//Subpackage used for all variations of hashing algorithms
//TODO: update this file to the other reposiroty? Added a new function, but don't know if it is useful
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"github.com/ThePiachu/Go/mymath/ripemd160"
"hash"
//"code.google.com/p/go.crypto/ripemd160"
)
func SHA512(b []byte) []byte {
var h hash.Hash = sha512.New()
h.Write(b)
return h.Sum(nil)
}
func SHA512String(s string) string {
b := Str2Hex(s)
sha := SHA512(b)
return Hex2Str(sha)
}
func SHA512Half(b []byte) []byte {
var h hash.Hash = sha512.New()
h.Write(b)
return h.Sum(nil)[0:32]
}
func SHA512HalfString(s string) string {
b := Str2Hex(s)
sha := SHA512Half(b)
return Hex2Str(sha)
}
//TODO: test
func ReverseByteOrder(b []byte) []byte {
//reversing the order of bits 4 at a time
//https://bitcointalk.org/index.php?topic=51281.msg742754#msg742754
if len(b)%4 != 0 {
return nil
}
answer := make([]byte, len(b))
for i := 0; i < len(b)/4; i++ {
answer[i*4+0] = b[i*4+3]
answer[i*4+1] = b[i*4+2]
answer[i*4+2] = b[i*4+1]
answer[i*4+3] = b[i*4+0]
}
return answer
}
//TODO: test and add to tests
//double SHA-256 hashing of a single byte array
func DoubleSHA(b []byte) []byte {
var h hash.Hash = sha256.New()
h.Write(b)
var h2 hash.Hash = sha256.New()
h2.Write(h.Sum(nil))
return h2.Sum(nil)
}
//TODO: test and add to tests
//reverse double SHA-256 hashing of a single byte array
func DoubleSHARev(b []byte) []byte {
return Rev(DoubleSHA(Rev(b)))
}
//TODO: test and add to tests
//Single SHA-256 hashing of a single byte array
func SingleSHA(b []byte) []byte {
return SHA256(b)
}
//TODO: test and add to tests
//Single SHA-256 hashing of a single byte array
func SHA256(b []byte) []byte {
var h hash.Hash = sha256.New()
h.Write(b)
return h.Sum(nil)
}
func SHA256HexString(s string) string {
b := Str2Hex(s)
sha := SHA256(b)
return Hex2Str(sha)
}
func SHA256ASCIIString(s string) string {
b := ASCII2Hex(s)
sha := SHA256(b)
return Hex2Str(sha)
}
//TODO: test and add to tests
//Reversed single SHA-256 hashing of a single byte array
func SingleSHARev(b []byte) []byte {
return Rev(SingleSHA(Rev(b)))
}
//TODO: test and add to tests
//Single SHA-1 hashing of a single byte array
func SingleSHA1(b []byte) []byte {
var h hash.Hash = sha1.New() //TODO: double check
h.Write(b)
return h.Sum(nil)
}
//Reversed SHA-1 hashing of a single byte array
//TODO: test and add to tests
func SingleSHA1Rev(b []byte) []byte {
return Rev(SingleSHA1(Rev(b))) //TODO: double check
}
//TODO: test and add to tests
//double hash input bytes, double hash their concatanation
func DoubleDoubleSHA(a []byte, b []byte) []byte {
var tmp []byte
//hash first input
var h1 hash.Hash = sha256.New()
h1.Write(a)
//hash the first input the second time
tmp = h1.Sum(nil)
h1 = sha256.New()
h1.Write(tmp)
//hash the second input
var h2 hash.Hash = sha256.New()
h2.Write(b)
//hash the second input the second time
tmp = h2.Sum(nil)
h2 = sha256.New()
h2.Write(tmp)
//hash the concatenation of the double hashes of both inputs
var answer hash.Hash = sha256.New()
answer.Write(append(h1.Sum(nil), h2.Sum(nil)...))
//double hash the concatenation
tmp = answer.Sum(nil)
answer = sha256.New()
answer.Write(tmp)
return answer.Sum(nil) //return result
}
//TODO: test and add to tests
//double SHA-256 hash of a concatenation of the two inputs
func DoubleSHAPair(a []byte, b []byte) []byte {
var tmp []byte
//hash the concatenation of the two inputs
var answer hash.Hash = sha256.New()
answer.Write(append(a, b...))
//hash it the second time
tmp = answer.Sum(nil)
answer = sha256.New()
answer.Write(tmp)
return answer.Sum(nil) //return
}
//TODO: test and add to tests
//double SHA-256 hash of a concatenation of the reverse of two inputs
func DoubleSHAPairRev(a []byte, b []byte) []byte {
var tmp []byte
//hash the concatenation of the reverse of both inputs
var answer hash.Hash = sha256.New()
//answer.Write(append(a[:]+b[:]))
answer.Write(append(Rev(a), Rev(b)...))
//hash it again
tmp = answer.Sum(nil)
answer = sha256.New()
answer.Write(tmp)
return Rev(answer.Sum(nil)) //return the reverse of the output
}
//TODO: test and add to tests
//SHA-256 RIPEMD-160 operation for bitcoin address hashing
func SHARipemd(b []byte) []byte {
//sha hashing of the input
var h hash.Hash = sha256.New()
h.Write(b)
//ripemd hashing of the sha hash
var h2 hash.Hash = ripemd160.New()
h2.Write(h.Sum(nil))
return h2.Sum(nil) //return
}
//TODO: test and add to tests
//reverse SHA-256 RIPEMD-160 hash
func SHARipemdRev(b []byte) []byte {
return Rev(SHARipemd(Rev(b)))
}
//TODO: test and add to tests
//RIPEMD-160 operation for bitcoin address hashing
func Ripemd(b []byte) []byte {
//ripemd hashing of the sha hash
var h hash.Hash = ripemd160.New()
h.Write(b)
return h.Sum(nil) //return
}
//TODO: test and add to tests
//reverse RIPEMD-160 hash
func RipemdRev(b []byte) []byte {
return Rev(Ripemd(Rev(b)))
}
func MD5(b []byte) []byte {
var h hash.Hash = md5.New()
h.Write(b)
return h.Sum(nil) //return
}
func MD5String(s string) string {
b := Str2Hex(s)
md5 := MD5(b)
return Hex2Str(md5)
}
func HMACSHA512(key, data []byte) []byte {
//As used by Just-dice
h := hmac.New(sha512.New, key)
h.Write(data)
return h.Sum(nil)
}