简体   繁体   English

如何使用Bcrypt存储用户密码

[英]How do I store user password with Bcrypt

I am designing a php website, and I used sha1 to store password for the users, but I later read that sha1 is unsafe, Its better i use Bcrypt, now I try to find about Bcrypt but these questions - How do you use bcrypt for hashing.. and Is Bcrypt used for Hashing is too complex, I dont understand what they explain. 我正在设计一个php网站,我使用sha1为用户存储密码,但是后来我读到sha1是不安全的,最好使用Bcrypt,现在我尝试查找有关Bcrypt的信息,但是这些问题- 您如何使用bcrypt哈希..Bcrypt用于哈希是太复杂了,我不明白他们的解释。

<?php $pass = sha1($_POST["password"]); ?>

but could it be: 但可能是:

<?php $pass = bcrypt($_POST["password"]); ?>

or which is better than both. 还是两者都好。 Thanks 谢谢

If you are using PHP version 5.5+, you may use the method password_hash(), and password_verify(); 如果您使用的是PHP 5.5以上版本,则可以使用password_hash()和password_verify()方法;

EXAMPLE: 例:

$hash = password_hash("mypassword", PASSWORD_BCRYPT);

and to verify: 并验证:

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

This is the best and most secured in PHP today since the salt is built-in inside the method. 这是当今PHP中最好,最安全的方法,因为方法内置了salt函数。

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

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