goProject/.svn/pristine/b6/b6109d1a9358a108e9893e6373d6367d7b5bd097.svn-base
2025-01-06 16:21:36 +08:00

64 lines
1.3 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package securityUtil
import (
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
)
// Hmac-SHA1编码
// source编码原数据
// key编码密钥
// 返回值:编码结果
func HmacSha1(source, key string) (result []byte, err error) {
mac := hmac.New(sha1.New, []byte(key))
if _, err = mac.Write([]byte(source)); err != nil {
return
}
return mac.Sum(nil), nil
}
// Hmac-SHA1 Base64编码
// source编码原数据
// key编码密钥
// 返回值:编码结果
func Base64HmacSha1(source, key string) (result string, err error) {
mac := hmac.New(sha1.New, []byte(key))
if _, err = mac.Write([]byte(source)); err != nil {
return
}
bytes := mac.Sum(nil)
result = base64.StdEncoding.EncodeToString(bytes)
return
}
// Hmac-SHA256编码
// source编码原数据
// key编码密钥
// 返回值:编码结果
func HmacSha256(source, key string) (result []byte, err error) {
mac := hmac.New(sha256.New, []byte(key))
if _, err = mac.Write([]byte(source)); err != nil {
return
}
return mac.Sum(nil), nil
}
// Hmac-SHA512编码
// source编码原数据
// key编码密钥
// 返回值:编码结果
func HmacSha512(source, key string) (result []byte, err error) {
mac := hmac.New(sha512.New, []byte(key))
if _, err = mac.Write([]byte(source)); err != nil {
return
}
return mac.Sum(nil), nil
}