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.
53 lines
1.4 KiB
53 lines
1.4 KiB
package crypto
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
)
|
|
|
|
// Sha1Encode return the sha1 value (SHA-1 hash algorithm) of string
|
|
func (c *Crypto) Sha1Encode(data string) string {
|
|
sha1 := sha1.New()
|
|
sha1.Write([]byte(data))
|
|
return hex.EncodeToString(sha1.Sum([]byte("")))
|
|
}
|
|
|
|
// Sha256Encode Sha256加密
|
|
func (c *Crypto) Sha256Encode(src string) string {
|
|
m := sha256.New()
|
|
m.Write([]byte(src))
|
|
res := hex.EncodeToString(m.Sum(nil))
|
|
return res
|
|
}
|
|
|
|
// Sha512Encode return the sha512 value (SHA512 hash algorithm) of string
|
|
func (c *Crypto) Sha512Encode(data string) string {
|
|
sha512 := sha512.New()
|
|
sha512.Write([]byte(data))
|
|
return hex.EncodeToString(sha512.Sum([]byte("")))
|
|
}
|
|
|
|
// Sha1Hmac return the hmac hash of string use sha1
|
|
func (c *Crypto) Sha1Hmac(data, key string) string {
|
|
h := hmac.New(sha1.New, []byte(key))
|
|
h.Write([]byte(data))
|
|
return hex.EncodeToString(h.Sum([]byte("")))
|
|
}
|
|
|
|
// Sha256Hmac return the hmac hash of string use sha256
|
|
func (c *Crypto) Sha256Hmac(data, key string) string {
|
|
h := hmac.New(sha256.New, []byte(key))
|
|
h.Write([]byte(data))
|
|
return hex.EncodeToString(h.Sum([]byte("")))
|
|
}
|
|
|
|
// Sha512Hmac return the hmac hash of string use sha512
|
|
func (c *Crypto) Sha512Hmac(data, key string) string {
|
|
h := hmac.New(sha512.New, []byte(key))
|
|
h.Write([]byte(data))
|
|
return hex.EncodeToString(h.Sum([]byte("")))
|
|
}
|