12345678910111213141516171819202122232425262728293031323334 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package util
- import (
- "crypto/hmac"
- "crypto/md5"
- "crypto/sha1"
- "crypto/sha256"
- "encoding/hex"
- )
- // MD5 将字符串 s 进行 md5 编码
- func MD5(s string) string {
- m := md5.New()
- m.Write([]byte(s))
- return hex.EncodeToString(m.Sum(nil))
- }
- // SHA256 将字符串 s 进行 SHA256 编码
- func SHA256(s string) string {
- h := sha256.New()
- h.Write([]byte(s))
- return hex.EncodeToString(h.Sum(nil))
- }
- // HMACBySHA1 将字符串 s 进行 以SHA1算法生成 HMAC (哈希运算消息认证码)
- func HMACBySHA1(s, key string) []byte {
- bkey := []byte(key)
- mac := hmac.New(sha1.New, bkey)
- mac.Write([]byte(s))
- return mac.Sum(nil)
- }
|