MD5
import (
"crypto/md5"
"fmt"
)
func MD5(str string, salt string) string {
data := []byte(str + salt) //切片
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制
return md5str
}
Hash
import (
"golang.org/x/crypto/bcrypt"
)
// 生成密码
func GenPwd(pwd string) ([]byte, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost) //加密处理
return hash, err
}
// 比对密码
func ComparePwd(pwd1 string, pwd2 string) bool {
err := bcrypt.CompareHashAndPassword([]byte(pwd1), []byte(pwd2))
if err != nil {
return false
} else {
return true
}
}`