-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmac.go
37 lines (32 loc) · 952 Bytes
/
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
package icrypto
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
)
// HmacSha1ToBase64 HMAC-SHA1
func HmacSha1ToBase64(rawStr, key string) string {
hmacHash := hmac.New(sha1.New, []byte(key))
hmacHash.Write([]byte(rawStr))
return base64.StdEncoding.EncodeToString(hmacHash.Sum(nil))
}
// HmacSha256ToBase64 HMAC-SHA256
func HmacSha256ToBase64(rawStr, key string) string {
hmacHash := hmac.New(sha256.New, []byte(key))
hmacHash.Write([]byte(rawStr))
return base64.StdEncoding.EncodeToString(hmacHash.Sum(nil))
}
// HmacSha1ToHex
func HmacSha1ToHex(rawStr, key string) string {
hmacHash := hmac.New(sha1.New, []byte(key))
hmacHash.Write([]byte(rawStr))
return hex.EncodeToString(hmacHash.Sum(nil))
}
// HmacSha256ToHex
func HmacSha256ToHex(rawStr, key string) string {
hmacHash := hmac.New(sha256.New, []byte(key))
hmacHash.Write([]byte(rawStr))
return hex.EncodeToString(hmacHash.Sum(nil))
}