28 lines
605 B
Go
28 lines
605 B
Go
package hash
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// BcryptHash 使用bcrypt对密码进行加密
|
|
func BcryptHash(password string) string {
|
|
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(bytes)
|
|
}
|
|
|
|
// BcryptCheck 对比明文密码和数据库的哈希值
|
|
func BcryptCheck(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
// MD5 计算MD5哈希值
|
|
func MD5(str []byte) string {
|
|
h := md5.New()
|
|
h.Write(str)
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|