gpt4 book ai didi

hash - 加密/bcrypt : hashedPassword is not the hash of the given password

转载 作者:IT王子 更新时间:2023-10-29 01:09:03 29 4
gpt4 key购买 nike

我加密用户的密码并保存到数据库。然后用户登录,比较散列密码和普通密码,我得到 crypto/bcrypt: hashedPassword is not the hash of the given password 错误。怎么了?

func encryptPassword(password string) (string, error) {
bytePass := []byte(password)
hashedPassword, err := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
if err != nil {
log.Printf("ERROR:EncryptPassword: %s", err.Error())
}
return string(hashedPassword), err
}

func (i *Impl) Register(user User) bool {
hashedPass, err := encryptPassword(user.Password)
if err != nil {
return false
}

user.Password = hashedPass

if err := i.DB.Create(&user).Error; err != nil {
log.Printf("ERROR:Register: %s", err.Error())
return false
}
return true
}

func (i *Impl) Login(email string, password string) (User, error) {
var user User
i.DB.Where("email = ?", email).First(&user)

err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
log.Printf("ERROR:Login: %s", err.Error())
return User{}, err
}

return user, err
}

最佳答案

我敢打赌,在将 user.Password 传递给 encryptPassword 之前,您的 Register 函数中的 user.Password 是空的,因此会导致对空密码进行哈希处理就像您提供的一样 ($2a$10$rqHJJTHsxMbtX/5ZjG1mFuWyYbUDW1PLbfwQRN0uChwes38c/0m3e)。

关于hash - 加密/bcrypt : hashedPassword is not the hash of the given password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35556253/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com