通常如果我有密码,我会使用这个伪代码:
$password = "this is the user's password";
/***/
$salt = GenerateSalt();
$hash = Hash($password);
$hash = Hash($hash . $salt);
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,PHP有一个crypt()函数,它接受盐以及特定算法的迭代次数.显然你是......应该将返回的哈希值作为盐传递crypt 回来 crypt.我不明白.
任何人都可以澄清一下如何工作?我是否还需要添加自己的盐并重新加入?在这种情况下,我是否只使用固定的salt作为crypt,然后为每个用户生成一个单独的crypt?或者crypt的$salt参数是否为我处理?
输出crypt包括:
当您将此输出als"salt"传回时crypt,它将提取正确的算法和salt,并将其用于操作.如果只提到一个算法,它会使用这个算法并生成随机盐.否则,它将选择默认算法并生成随机盐.hash传递的salt参数中的部分将被忽略.
因此,您可以简单地将stored_hash与crypt(密码,stored_hash)进行比较 - 如果它相等,则很可能是正确的密码.
这是一个伪代码解释(在类似PHP的语法中)crypt如何工作:
function crypt($password, $salt)
{
if (substr($salt,0 1) == "_") {
$count = substr($salt, 1, 4);
$real_salt = substr($salt, 5, 4);
return "_" . $count . $real_salt . crypt_ext_des($password, $count, $salt);
}
if(substr($salt, 0, 3) == "$1$") {
list($ignored, $real_salt, $ignored) = explode("$", $salt);
return "$1$" . $real_salt . "$" . crypt_md5($password, $real_salt);
}
if(substr($salt, 0, 4) == "$2a$") {
$cost = substr($salt, 4, 2);
$real_salt = substr($salt, 7, 22);
return "$2a$" . $cost . "$" . $real_salt . crypt_brypt($password, $real_salt, $cost);
}
// ... SHA256 and SHA512 analogons
// no match => STD_DES
$real_salt = substr($salt, 0, 2);
return $real_salt . crypt_std_des($password, $real_salt);
}
Run Code Online (Sandbox Code Playgroud)
然后,各个crypt_xxx函数执行实际工作,具体取决于算法.(实际上,在本说明书中缺少随机盐的生成.如果$ real_salt为空,将会执行.)
| 归档时间: |
|
| 查看次数: |
1433 次 |
| 最近记录: |