doubo6658 2015-11-13 01:01
浏览 456

BCrypt比较两个哈希不相等

I have this code:

u := models.Users{}

u = u.FindByEmail(login.Email)

password := []byte(login.Password)

hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost) if err != nil { panic(err) }

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

I end up getting this error: crypto/bcrypt: hashedPassword is not the hash of the given password

However I previously saved my model to have the same hash as "admin", but when I run my application, it tells me it is not equal.

  • 写回答

1条回答 默认 最新

  • doumao1047 2015-11-13 02:16
    关注

    Re-read the docs carefully.

    CompareHashAndPassword compares a bcrypt hashed password with its possible plaintext equivalent. Returns nil on success, or an error on failure.

    Basically, it is saying that you should compare the hash you have stored against the plain text password.

    you probably want:

    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)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏