-
Notifications
You must be signed in to change notification settings - Fork 69
/
hmac.go
180 lines (164 loc) · 3.46 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
178
179
180
package dongle
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"github.com/dromara/dongle/md2"
"github.com/emmansun/gmsm/sm3"
"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/sha3"
)
type HmacError struct {
}
func NewHmacError() HmacError {
return HmacError{}
}
func (e HmacError) SizeError() error {
return fmt.Errorf("hmac: invalid size, the size is unsupported")
}
// ByHmacMd2 encrypts by hmac with md2.
func (e Encrypter) ByHmacMd2(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(md2.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacMd4 encrypts by hmac with md4.
func (e Encrypter) ByHmacMd4(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(md4.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacMd5 encrypts by hmac with md5.
func (e Encrypter) ByHmacMd5(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(md5.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha1 encrypts by hmac with sha1.
func (e Encrypter) ByHmacSha1(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha1.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha3 encrypts by hmac with sha3.
func (e Encrypter) ByHmacSha3(key []byte, 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:
hmacError := HmacError{}
e.Error = hmacError.SizeError()
return e
}
h := hmac.New(f, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha224 encrypts by hmac with sha224.
func (e Encrypter) ByHmacSha224(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha256.New224, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha256 encrypts by hmac with sha256.
func (e Encrypter) ByHmacSha256(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha256.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha384 encrypts by hmac with sha384.
func (e Encrypter) ByHmacSha384(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sha512.New384, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSha512 encrypts by hmac with sha512.
func (e Encrypter) ByHmacSha512(key []byte, 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:
hmacError := HmacError{}
e.Error = hmacError.SizeError()
return e
}
h := hmac.New(f, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacRipemd160 encrypts by hmac with ripemd160.
func (e Encrypter) ByHmacRipemd160(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(ripemd160.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}
// ByHmacSm3 encrypts by hmac with sm3.
func (e Encrypter) ByHmacSm3(key []byte) Encrypter {
if len(e.src) == 0 || e.Error != nil {
return e
}
h := hmac.New(sm3.New, key)
h.Write(e.src)
e.dst = h.Sum(nil)
return e
}