Tab*_*Man 2 php encryption hash
所以,我使用以下代码成功地将密码加密为密码哈希:
class PassHash
{
// blowfish
private static $algo = '$2a';
// cost parameter
private static $cost = '$10';
// mainly for internal use
public static function unique_salt()
{
return substr(sha1(mt_rand()), 0, 22);
}
// this will be used to generate a hash
public static function hash($password)
{
return crypt($password, self::$algo .
self::$cost .
'$' . self::unique_salt());
}
// this will be used to compare a password against a hash
public static function check_password($hash, $password)
{
$full_salt = substr($hash, 0, 29);
$new_hash = crypt($password, $full_salt);
return ($hash == $new_hash);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我加密密码的方式:
$password_hash = PassHash::hash($user->getPasswordHash());
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在正常模式下显示密码时,我现在遇到了一些问题.
从该哈希解密密码的最佳方法是什么?
你不能解密一个哈希(嗯...... 技术上你可以,但你不应该),这是什么哈希(不被解密).您需要使用与存储哈希相同的哈希算法对您收到的密码进行加密(哈希),并将哈希值与彼此进行比较.
$password_hash = PassHash::hash($user->getPasswordHash());
if($stored_password === $password_hash){
//The passwords are the same
}
Run Code Online (Sandbox Code Playgroud)
总而言之,你不想让任何人(甚至不是你自己)知道用户的密码是什么(或者那个问题的哈希).用户会知道,因为他进入并记住它(希望无论如何).没有其他人与查看用户的密码/哈希有任何关系.让除了用户之外的任何人看到/知道密码/哈希是一个严重的安全问题.
另请注意:您应该使用哈希的默认实现.使用您自己的哈希算法总是比真正经过试验和测试的方法更糟糕.我不确定你使用的是哪个PHP版本,但是从PHP 5.5开始你可以使用password_hash()
.有关更多信息,请查看此问题.
归档时间: |
|
查看次数: |
1849 次 |
最近记录: |