河豚和地穴成功的例子?

时间:2012-09-02 20:10:27

标签: php encryption blowfish

我正在阅读有关保护密码的信息,并希望获得有关我的代码是否正确的反馈。我正在尝试使用blowfish和crypt()来防止任何人解密密码。另外,我有一个问题。当我存储密码时,我假设我还必须存储变量$string,以便我可以在登录时再次使用它来验证用户,对吗?

function unique_md5() {
  mt_srand(microtime(true)*100000 + memory_get_usage(true));
  return md5(uniqid(mt_rand(), true));
}

//unique_md5 returns a random 16 character string

$string = '$2a$07$' . unique_md5();

$password = 'password';

$password = trim($password);

$protected_password = crypt($password, $string); 

//then store the variables $string and $protected_password into the database

2 个答案:

答案 0 :(得分:3)

Blowfish为它的盐取一个22个字符的字符串(不包括类型和成本参数)。 MD5返回 32 字符串,crypt_blowfish不接受该字符串。使用适当的盐。

答案 1 :(得分:2)

不,您不需要使用加密密码存储salt,因为加密的密码将返回给您,并且已经预先添加了salt。当您验证明文密码是否与加密密码匹配时,您只需要检查$crypted_password == crypt($plaintext_password, $crypted_password)是否。