Laravel 5:在同一字符串上使用bcrypt会给出不同的值 [英] Laravel 5: using bcrypt on same string gives different values
问题描述
我正在使用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屋!