/
hash.go
198 lines (169 loc) · 4.28 KB
/
hash.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
package tool
import (
"hash"
"errors"
"crypto"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/sha3"
"golang.org/x/crypto/blake2s"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/ripemd160"
"github.com/deatil/go-cryptobin/hash/sm3"
"github.com/deatil/go-cryptobin/hash/md2"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012256"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012512"
)
type (
// HashFunc
HashFunc = func() hash.Hash
)
var (
newBlake2s_256 = func() hash.Hash {
h, _ := blake2s.New256(nil)
return h
}
newBlake2b_256 = func() hash.Hash {
h, _ := blake2b.New256(nil)
return h
}
newBlake2b_384 = func() hash.Hash {
h, _ := blake2b.New384(nil)
return h
}
newBlake2b_512 = func() hash.Hash {
h, _ := blake2b.New512(nil)
return h
}
)
// hash 官方默认
var cryptoHashes = map[string]crypto.Hash{
"MD4": crypto.MD4,
"MD5": crypto.MD5,
"SHA1": crypto.SHA1,
"SHA224": crypto.SHA224,
"SHA256": crypto.SHA256,
"SHA384": crypto.SHA384,
"SHA512": crypto.SHA512,
"RIPEMD160": crypto.RIPEMD160,
"SHA3_224": crypto.SHA3_224,
"SHA3_256": crypto.SHA3_256,
"SHA3_384": crypto.SHA3_384,
"SHA3_512": crypto.SHA3_512,
"SHA512_224": crypto.SHA512_224,
"SHA512_256": crypto.SHA512_256,
"BLAKE2s_256": crypto.BLAKE2s_256,
"BLAKE2b_256": crypto.BLAKE2b_256,
"BLAKE2b_384": crypto.BLAKE2b_384,
"BLAKE2b_512": crypto.BLAKE2b_512,
}
// 摘要函数列表
var funcHashes = map[string]HashFunc{
"MD2": md2.New,
"MD4": md4.New,
"MD5": md5.New,
"SHA1": sha1.New,
"SHA224": sha256.New224,
"SHA256": sha256.New,
"SHA384": sha512.New384,
"SHA512": sha512.New,
"RIPEMD160": ripemd160.New,
"SHA3_224": sha3.New224,
"SHA3_256": sha3.New256,
"SHA3_384": sha3.New384,
"SHA3_512": sha3.New512,
"SHA512_224": sha512.New512_224,
"SHA512_256": sha512.New512_256,
"BLAKE2s_256": newBlake2s_256,
"BLAKE2b_256": newBlake2b_256,
"BLAKE2b_384": newBlake2b_384,
"BLAKE2b_512": newBlake2b_512,
"SM3": sm3.New,
"GOST34112012256": gost34112012256.New,
"GOST34112012512": gost34112012512.New,
}
// 类型
func GetCryptoHash(typ string) (crypto.Hash, error) {
sha, ok := cryptoHashes[typ]
if ok {
return sha, nil
}
return 0, errors.New("hash type is not support")
}
// 签名后数据
func CryptoHashSum(typ string, slices ...[]byte) ([]byte, error) {
sha, err := GetCryptoHash(typ)
if err != nil {
return nil, err
}
h := sha.New()
for _, slice := range slices {
h.Write(slice)
}
return h.Sum(nil), nil
}
// 默认
var defaultHash = NewHash()
/**
* 摘要
*
* @create 2022-4-16
* @author deatil
*/
type Hash struct {
// hash 列表
hashes map[string]HashFunc
}
// 构造函数
func NewHash() *Hash {
sha := &Hash{}
sha.hashes = funcHashes
return sha
}
// 添加
func (this *Hash) AddHash(name string, h HashFunc) *Hash {
this.hashes[name] = h
return this
}
func AddHash(name string, h HashFunc) *Hash {
return defaultHash.AddHash(name, h)
}
// 类型
func (this *Hash) GetHash(typ string) (HashFunc, error) {
if h, ok := this.hashes[typ]; ok {
return h, nil
}
return nil, errors.New("hash type is not support")
}
func GetHash(typ string) (HashFunc, error) {
return defaultHash.GetHash(typ)
}
// 签名后数据
func (this *Hash) Sum(typ string, slices ...[]byte) ([]byte, error) {
fn, err := this.GetHash(typ)
if err != nil {
return nil, err
}
h := fn()
for _, slice := range slices {
h.Write(slice)
}
return h.Sum(nil), nil
}
func HashSum(typ string, slices ...[]byte) ([]byte, error) {
return defaultHash.Sum(typ, slices...)
}
// 列席名称列表
func (this *Hash) Names() []string {
names := make([]string, 0)
for name, _ := range this.hashes {
names = append(names, name)
}
return names
}
func HashNames() []string {
return defaultHash.Names()
}