You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
603 B
32 lines
603 B
|
1 year ago
|
package util
|
||
|
|
|
||
|
|
import (
|
||
|
|
"crypto/hmac"
|
||
|
|
"crypto/sha1"
|
||
|
|
"encoding/base64"
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
// sha1加密
|
||
|
|
func HMACSHA1(keyStr, value string) string {
|
||
|
|
|
||
|
|
key := []byte(keyStr)
|
||
|
|
mac := hmac.New(sha1.New, key)
|
||
|
|
mac.Write([]byte(value))
|
||
|
|
//进行base64编码
|
||
|
|
res := base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
||
|
|
|
||
|
|
return res
|
||
|
|
}
|
||
|
|
|
||
|
|
// HMACSHA1X 先转16进制
|
||
|
|
func HMACSHA1X(keyStr, value string) string {
|
||
|
|
key := []byte(keyStr)
|
||
|
|
mac := hmac.New(sha1.New, key)
|
||
|
|
mac.Write([]byte(value))
|
||
|
|
//进行base64编码
|
||
|
|
tmp := fmt.Sprintf("%x", mac.Sum(nil))
|
||
|
|
res := base64.StdEncoding.EncodeToString([]byte(tmp))
|
||
|
|
return res
|
||
|
|
}
|