简体   繁体   English

Bcrypt,您如何用随机盐验证?

[英]Bcrypt, how do you verify with the random salt?

I got this code off the PHP website. 我从PHP网站获得了此代码。 I can make this work without the Salt. 我可以在没有盐的情况下完成这项工作。 But how do you verify with the salt - or does it have to be stored into a variable then you use that later? 但是如何用盐验证-还是必须将其存储到变量中,然后再使用? Not sure how to proceed to the next step to verify. 不确定如何进行下一步进行验证。 Lots of tutorials on how to make a hash, but to verify is another thing. 关于如何进行散列但进行验证的许多教程是另一回事。 Thank you. 谢谢。

$options = [
'cost' => 11,
'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n";


// See the password_hash() example to see where this came from.
$hash = '$2y$11$nJp/w0OC41I0m44T9OQKBuWUrQi63PrJuvDc68KI6oDBdnZK01kiW ';

if (password_verify('rasmuslerdorf', $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}

Note that password_hash() returns the algorithm, cost and salt as part of the returned hash. 请注意,password_hash()返回算法,成本和盐值作为返回的哈希值的一部分。 Therefore, all information that's needed to verify the hash is included in it. 因此,其中包含验证哈希所需的所有信息。 This allows the verify function to verify the hash without needing separate storage for the salt or algorithm information. 这允许验证功能验证哈希,而无需单独存储盐或算法信息。

Source: http://php.net/manual/en/function.password-verify.php 资料来源: http : //php.net/manual/en/function.password-verify.php

just use the function as you did above, it will automatically detect the salt. 只需像上面一样使用该功能,它将自动检测盐分。

If omitted, a random salt will be generated by password_hash() for each password hashed. 如果省略,password_hash()将为散列的每个密码生成随机盐。 This is the intended mode of operation. 这是预期的操作模式。

source: http://php.net/manual/en/function.password-hash.php 来源: http : //php.net/manual/en/function.password-hash.php

Even if you don't add a salt, password_hash will automatically add a random generated one, so you shouldn't have any problem verifying a password that has been salted. 即使您不添加盐,password_hash也会自动添加随机生成的盐,因此验证已添加盐的密码不会有任何问题。

Also note that: 另请注意:

The salt option has been deprecated as of PHP 7.0.0. 从PHP 7.0.0起不建议使用salt选项。 It is now preferred to simply use the salt that is generated by default. 现在,最好仅使用默认情况下生成的盐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM