如何从sha256加密迁移到PHP的bcrypt? [英] How to migrate from sha256 encryption to bcrypt for PHP?
本文介绍了如何从sha256加密迁移到PHP的bcrypt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
用于登录:
$rows = $sql->fetch(PDO::FETCH_ASSOC);
$us_id = $rows['id'];
$us_pass = $rows['password'];
$us_salt = $rows['password_salt'];
$status = $rows['attempt'];
$saltedPass = hash('sha256', "{$password}{$this->passwordSalt}{$us_salt}");
注册:
$randomSalt = $this->rand_string(20);
$saltedPass = hash('sha256', "{$password}{$this->passwordSalt}{$randomSalt}");
如何将此sha256加密方法转换为bcrypt?
推荐答案
使用bcrypt进行密码散列
如果您使用的是PHP 5.5或更高版本,则可以使用内置的将 $ algo
参数设置为 PASSWORD_BCRYPT
的 password_hash()
函数创建bcrypt哈希.您可以这样使用:
If you are using PHP 5.5 or later, you can use the built-in password_hash()
function with the $algo
parameter set to PASSWORD_BCRYPT
to create bcrypt hashes. You can use this as so:
$options = array('cost' => 11, 'salt' => 'my_salt');
$hash = password_hash("my_secret_password", PASSWORD_BCRYPT, $options);
迁移
无法进行从sha256到bcrypt的批量迁移,因为您需要不可用的原始明文数据(密码).
It's not possible to do a bulk migration from sha256 to bcrypt because you need the original plaintext data (password) which isn't available.
通常,网站进行分阶段转换,您可以在用户执行成功登录时转换他们.例如:
Typically, sites do a staged conversion where you convert users as they perform successful logins. For example:
- 在数据库中为密码为sha256或bcrypt的类型创建一个字段
- 登录后,使用数据库中的类型验证密码
- 如果sha256成功,则使用输入的密码创建一个新的bcrypt条目,进行存储并将密码类型更新为bcrypt.下次登录时,现在将使用bcrypt进行验证.
这篇关于如何从sha256加密迁移到PHP的bcrypt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文