1 Star 0 Fork 0

szmaozi / go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
crypt.go 750 Bytes
一键复制 编辑 原始数据 按行查看 历史
szmaozi 提交于 2021-09-15 19:28 . wip: e, crypt
package utils
import (
"crypto/md5"
"fmt"
"golang.org/x/crypto/bcrypt"
)
func GeneSalt() string {
return RandomString(8)
}
//GenePwdHash 给密码就行加密操作
func GenPwdHash(userPassword string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hash), nil
}
//ValidatePwd 密码比对
func ValidatePwd(userPassword string, hashed string) bool {
if err := bcrypt.CompareHashAndPassword([]byte(hashed), []byte(userPassword)); err != nil {
return false
}
return true
}
func Hash(s string) string {
md5Inst := md5.New()
md5Inst.Write([]byte(s))
res := md5Inst.Sum(nil)
// return string(res)
return fmt.Sprintf("%x", res)
}
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

搜索帮助