feat:完善项目
This commit is contained in:
52
utils/encrypt.go
Normal file
52
utils/encrypt.go
Normal file
@ -0,0 +1,52 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// Md5String
|
||||
func Md5String(s string, salt ...string) string {
|
||||
h := md5.New()
|
||||
if len(salt) > 0 {
|
||||
s += strings.Join(salt, "")
|
||||
}
|
||||
h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Sha1String
|
||||
func Sha1String(s string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Sha256String
|
||||
func Sha256String(s string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Sha512String
|
||||
func Sha512String(s string) string {
|
||||
h := sha512.New()
|
||||
h.Write([]byte(s))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
func HashString(s []byte) string {
|
||||
hash, _ := bcrypt.GenerateFromPassword(s, bcrypt.DefaultCost)
|
||||
return string(hash)
|
||||
}
|
||||
|
||||
func HashCompare(src, compare []byte) bool {
|
||||
return bcrypt.CompareHashAndPassword(src, compare) == nil
|
||||
}
|
Reference in New Issue
Block a user