-
Notifications
You must be signed in to change notification settings - Fork 68
/
hmac.go
177 lines (164 loc) · 3.76 KB
/
hmac.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
package dongle
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"github.com/emmansun/gmsm/sm3"
"github.com/golang-module/dongle/md2"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/sha3"
)
// ByHmacMd2 encrypts by hmac with md2.
// 通过 hmac-md2 加密
func (e Encrypter) ByHmacMd2(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(md2.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacMd4 encrypts by hmac with md4.
// 通过 hmac-md4 加密
func (e Encrypter) ByHmacMd4(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(md4.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacMd5 encrypts by hmac with md5.
// 通过 hmac-md5 加密
func (e Encrypter) ByHmacMd5(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(md5.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha1 encrypts by hmac with sha1.
// 通过 hmac-sha1 加密
func (e Encrypter) ByHmacSha1(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha1.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha3 encrypts by hmac with sha3.
// 通过 hmac-sha3 加密
func (e Encrypter) ByHmacSha3(key interface{}, size int) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
var f func() hash.Hash
switch size {
case 224:
f = sha3.New224
case 256:
f = sha3.New256
case 384:
f = sha3.New384
case 512:
f = sha3.New512
default:
e.Error = invalidHashSizeError()
return e
}
h := hmac.New(f, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha224 encrypts by hmac with sha224.
// 通过 hmac-sha224 加密
func (e Encrypter) ByHmacSha224(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha256.New224, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha256 encrypts by hmac with sha256.
// 通过 hmac-sha256 加密
func (e Encrypter) ByHmacSha256(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha256.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha384 encrypts by hmac with sha384.
// 通过 hmac-sha384 加密
func (e Encrypter) ByHmacSha384(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha512.New384, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha512 encrypts by hmac with sha512.
// 通过 hmac-sha512 加密
func (e Encrypter) ByHmacSha512(key interface{}, size ...int) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
var f func() hash.Hash
if len(size) == 0 {
size = []int{512}
}
switch size[0] {
case 512:
f = sha512.New
case 224:
f = sha512.New512_224
case 256:
f = sha512.New512_256
default:
e.Error = invalidHashSizeError()
return e
}
h := hmac.New(f, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacRipemd160 encrypts by hmac with ripemd160.
// 通过 hmac-ripemd160 加密
func (e Encrypter) ByHmacRipemd160(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(ripemd160.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSm3 encrypts by hmac with sm3.
// 通过 hmac-sm3 加密
func (e Encrypter) ByHmacSm3(key interface{}) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sm3.New, interface2bytes(key))
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}