Laravel 5:在同一字符串上使用 bcrypt 会给出不同的值 [英] Laravel 5: using bcrypt on same string gives different values

查看:15
本文介绍了Laravel 5:在同一字符串上使用 bcrypt 会给出不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Laravel 的 bcrypt 函数对密码进行散列.当我这样做时,

I am using Laravel's bcrypt function for hashing passwords. When I do,

bcrypt('secret')

我明白

=> "$2y$10$mnPgYt2xm9pxb/c2I.SH.uuhgrOj4WajDQTJYssUbTjmPOcgQybcu"

但是如果我再次运行它,我会得到

But if I run it again, I get

=> "$2y$10$J8h.Xmf6muivJ4bDweUlcu/BaNzI2wlBiAcop30PbPoKa0kDaf9xi"

等等...

那么,如果我每次得到不同的值,密码匹配过程会不会失败?

So, won't the password matching process fail if I get different values every time?

推荐答案

这就是 bcrypt 应该如何工作的.请参阅维基百科.

This is how bcrypt is supposed to work. See wikipedia.

Bcrypt 在散列期间生成随机的 128 位盐.这个 salt 成为散列的一部分,因此我们总是为相同的输入字符串获得不同的散列值.随机盐实际上用于阻止暴力攻击.

Bcrypt generates a random 128-bit salt during hashing. This salt becomes part of the hash, hence we always get a different hash value for the same input string. The random salt is actually used to deter brute-force attacks.

密码匹配过程不会因为哈希值不同而失败.在 tinker

The password matching process won't fail due to different values of hashes. Try the following in tinker

$hash1 = bcrypt('secret')
$hash2 = bcrypt('secret')

Hash::check('secret', $hash1)
Hash::check('secret', $hash2)

你应该在 Hash::check 的两种情况下都得到 true.

You should get true in both the cases of Hash::check.

所以即使哈希值不同,密码匹配也不会失败.

So even if the hash values are different, the password matching won't fail.

这篇关于Laravel 5:在同一字符串上使用 bcrypt 会给出不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆