gpt4 book ai didi

hash - BCrypt 比较两个哈希值不相等

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

我有这个代码:

u := models.Users{}

u = u.FindByEmail(登录.Email)

密码 := []byte(登录名.密码)

哈希密码,错误:= bcrypt.GenerateFromPassword(密码,bcrypt.DefaultCost) 如果错误!=无{ panic (错误)

错误 = bcrypt.CompareHashAndPassword(hashedPassword, []byte(u.Password)) fmt.Println(错误)

我最终得到这个错误:crypto/bcrypt: hashedPassword is not the hash of the given password

但是我之前保存我的模型以与“admin”具有相同的散列,但是当我运行我的应用程序时,它告诉我它不相等。

最佳答案

重新阅读docs仔细。

CompareHashAndPassword 将 bcrypt 散列密码与其可能的明文等价物进行比较。成功时返回 nil,失败时返回错误。

基本上,它是说您应该将存储的哈希值与明文密码进行比较。

你可能想要:

u := models.Users{}

u = u.FindByEmail(login.Email)

plainPassword := []byte(login.Password)
// Assumes that u.Password is the actual hash and that you didn't store plain text password.
err = bcrypt.CompareHashAndPassword([]byte(u.Password), plainPassword)

fmt.Println(err)

关于hash - BCrypt 比较两个哈希值不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684259/

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