十一周作业
func getCryptPwd(userId string, password string) (string, error) {
pwd, err := bcrypt.GenerateFromPassword([]byte(userId + password),
bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(pwd), err
}
func checkPW(userId string, password string, cryptPwd string) bool {
err := bcrypt.CompareHashAndPassword([]byte(cryptPwd), []byte(userId +
password))
if err != nil {
return false
}
return true
}
func main() {
userId := "zhang"
password := "123456#$"
cryptPwd, _ := getCryptPwd(userId, password)
ret := checkPW(userId, password, cryptPwd)
if ret {
fmt.Println("pass")
} else {
fmt.Println("fail")
}
}
评论