-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmac.go
51 lines (44 loc) · 1.15 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
package securityutils
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"fmt"
)
//hmac-md5单向秘钥key加密 32位
func HmacMd5(src string, key string) string {
hash := hmac.New(md5.New, []byte(key))
hash.Write([]byte(src))
return fmt.Sprintf("%x", hash.Sum(nil))
}
//hmac-md5单向秘钥key加密
func HmacMd5Byte(src []byte, key []byte) []byte {
hash := hmac.New(md5.New, key)
hash.Write(src)
return hash.Sum(nil)
}
//hmac-sha1单向秘钥key加密 40位
func HmacSha1(src string, key string) string {
hash := hmac.New(sha1.New, []byte(key))
hash.Write([]byte(src))
return fmt.Sprintf("%x", hash.Sum(nil))
}
//hmac-sha1单向秘钥key加密
func HmacSha1Byte(src []byte, key []byte) []byte {
hash := hmac.New(sha1.New, key)
hash.Write(src)
return hash.Sum(nil)
}
//hmac-sha256单向秘钥key加密 64位
func HmacSha256(src string, key string) string {
hash := hmac.New(sha256.New, []byte(key))
hash.Write([]byte(src))
return fmt.Sprintf("%x", hash.Sum(nil))
}
//hmac-sha256单向秘钥key加密
func HmacSha256Byte(src []byte, key []byte) []byte {
hash := hmac.New(sha256.New, key)
hash.Write(src)
return hash.Sum(nil)
}