-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhmac.go
50 lines (41 loc) · 1.09 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
package crypto4go
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
)
func HmacMD5(plaintext, key []byte) []byte {
var h = hmac.New(md5.New, key)
h.Write(plaintext)
return h.Sum(nil)
}
func HmacMD5String(plaintext, key string) string {
return hex.EncodeToString(HmacMD5([]byte(plaintext), []byte(key)))
}
func HmacSHA1(plaintext, key []byte) []byte {
var h = hmac.New(sha1.New, key)
h.Write(plaintext)
return h.Sum(nil)
}
func HmacSHA1String(plaintext, key string) string {
return hex.EncodeToString(HmacSHA1([]byte(plaintext), []byte(key)))
}
func HmacSHA256(plaintext, key []byte) []byte {
var h = hmac.New(sha256.New, key)
h.Write(plaintext)
return h.Sum(nil)
}
func HmacSHA256String(plaintext, key string) string {
return hex.EncodeToString(HmacSHA256([]byte(plaintext), []byte(key)))
}
func HmacSHA512(plaintext, key []byte) []byte {
var h = hmac.New(sha512.New, key)
h.Write(plaintext)
return h.Sum(nil)
}
func HmacSHA512String(plaintext, key string) string {
return hex.EncodeToString(HmacSHA512([]byte(plaintext), []byte(key)))
}