45 lines
741 B
Go
45 lines
741 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/sha256"
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
)
|
|
|
|
type hash struct{}
|
|
|
|
func Hash() hash {
|
|
return hash{}
|
|
}
|
|
|
|
// MD5
|
|
// @description: MD5摘要
|
|
// @param str
|
|
// @return string
|
|
func (hash) MD5(str string) string {
|
|
hs := md5.New()
|
|
hs.Write([]byte(str))
|
|
return hex.EncodeToString(hs.Sum(nil))
|
|
}
|
|
|
|
// SHA256
|
|
// @description: SHA256
|
|
// @param str
|
|
// @return string
|
|
func (hash) SHA256(str string) string {
|
|
hasher := sha256.New()
|
|
hasher.Write([]byte(str))
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
}
|
|
|
|
// SHA512
|
|
// @description: SHA512
|
|
// @param str
|
|
// @return string
|
|
func (hash) SHA512(str string) string {
|
|
hasher := sha512.New()
|
|
hasher.Write([]byte(str))
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
}
|