通常如果我有密码,我会使用这个伪代码:
$password = "this is the user's password";
/***/
$salt = GenerateSalt();
$hash = Hash($password);
$hash = Hash($hash . $salt);
但是,据我所知,PHP有一个crypt()
函数,它接受盐以及特定算法的迭代次数。显然你是......应该将crypt
的返回哈希值作为盐传递回 crypt
。我不明白这一点。
任何人都可以澄清地下室如何运作吗?我是否还需要添加自己的盐并重新加入?在这种情况下,我是否只使用固定的salt作为crypt,然后为每个用户生成一个单独的crypt?或者crypt的$salt
参数是否为我处理了这个问题?
答案 0 :(得分:5)
crypt
的输出包括:
当您将此输出als“salt”传递回crypt
时,它将提取正确的算法和salt,并将其用于操作。如果只提到一个算法,它会使用这个算法并生成随机盐。否则,它将选择默认算法并生成随机盐。传递的salt参数中的hash
部分将被忽略。
所以你可以简单地将你的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);
}
然后,各个crypt_xxx函数执行实际工作,具体取决于算法。 (实际上,在本说明书中缺少随机盐的生成。如果$ real_salt为空,将会执行。)
答案 1 :(得分:0)
crypt是单向散列,如MD5
像手册
中所述使用它<?php
$password = crypt('mypassword'); // let the salt be automatically generated
/* You should pass the entire results of crypt() as the salt for comparing a
password, to avoid problems when different hashing algorithms are used. (As
it says above, standard DES-based password hashing uses a 2-character salt,
but MD5-based hashing uses 12.) */
if (crypt($user_input, $password) == $password) {
echo "Password verified!";
}
?>