27

1) 如何使用 crypt() 创建安全的 Blowfish 密码散列?

$hash = crypt('somePassword', '$2a$07$nGYCCmhrzjrgdcxjH$');

1a) “$2a”的意义是什么?它只是表明应该使用 Blowfish 算法吗?
1b) “$07”的意义是什么?较高的值是否意味着更安全的哈希?
1c) “$nGYCCmhrzjrgdcxjH$”的意义是什么?这是要使用的盐吗?这应该是随机生成的吗?硬编码?

2) 你如何存储 Blowfish 哈希值?

echo $hash;
//Output: $2a$07$nGYCCmhrzjrgdcxjH$$$$.xLJMTJxaRa12DnhpAJmKQw.NXXZHgyq

2a) 其中哪一部分应该存储在数据库中?
2b) 列(MySQL)应该使用什么数据类型?

3)应该如何验证登录尝试?

4

2 回答 2

13

您应该存储 crypt 的整个输出,将其拆分没有太大意义,因为无论如何您都需要为每个要散列的密码生成一个新的盐。使用 Matt 提到的固定隐藏盐是错误的 - 每个散列的盐应该不同。

有关更多信息,请参阅http://www.openwall.com/articles/PHP-Users-Passwords - 我建议使用 phpass 库,因为它可以为您生成随机盐,这与 crypt() 不同。

于 2011-02-13T21:47:18.320 回答
0

1a) 加密强度 - 要求在 4..31 范围内。见http://php.net/manual/en/function.crypt.php

1b) 见 1a

1c) 见 1a。'salt' 不应该是随机的,否则您将无法为给定的输入重新生成相同的散列 - 请参阅 3。

2a) 严格来说,除了哈希之外的所有内容(以防数据库被破坏)。此外,将您的 salt 存储在 Web 服务器文档根目录下无法访问的文件中并将其包含在内。尽可能设置最严格的权限;理想情况下只读取网络主机服务(例如 apache),没有写入或执行权限。不太严格地说,取决于您希望对黑客的防御程度。不储存盐只会让生活更加困难;他们仍然必须正确地将数据输入到算法中——但为什么要让它更容易呢?

2b) VARCHAR(32) 应该适用于河豚,如果不存储哈希

3)假设您已经运行了正确的注入预防代码等。所以请不要盲目地复制下面的内容(最好使用PDO而不是mysql扩展)。下面是特定于河豚、SHA-256 和 SHA-512 的,它们都返回散列中的盐。需要修改其他算法...

//store this in another file outside web directory and include it
$salt = '$2a$07$somevalidbutrandomchars$'

...

//combine username + password to give algorithm more chars to work with
$password_hash = crypt($valid_username . $valid_password, $salt)

//Anything less than 13 chars is a failure (see manual)
if (strlen($password_hash) < 13 || $password_hash == $salt)
then die('Invalid blowfish result');

//Drop the salt from beginning of the hash result. 
//Note, irrespective of number of chars provided, algorithm will always 
//use the number defined in constant CRYPT_SALT_LENGTH
$trimmed_password_hash = substring($password_hash, CRYPT_SALT_LENGTH);
mysql_query("INSERT INTO `users` (username,p assword_hash) VALUES '$valid_username', '$trimmed_password_hash'");

...

$dbRes = mysql_query("SELECT password_hash FROM `users` WHERE username = '$user_input_username' LIMIT 1");
//re-apply salt to output of database and re-run algorithm testing for match
if (substring($salt, CRYPT_SALT_LENGTH) . mysql_result($dbRes, 0, 'password_hash') ) ===
        crypt($user_input_username . $user_input_password, $salt) ) {
    //... do stuff for validated user
}
于 2011-02-13T12:01:36.440 回答