1a) 加密强度 - 要求在4到31的范围内。请参见http://php.net/manual/en/function.crypt.php
1b) 参见1a
1c) 参见1a。'salt'不应该是随机的,否则您将无法为给定的输入重新生成相同的哈希值 - 请参见3。
2a) 严格来说,除了哈希(在数据库被攻击的情况下)。还要将盐存储在文件中,该文件不可在Web服务器文档根目录下访问,并包含它。使用最严格的权限设置它;理想情况下,只读取Web主机服务(例如Apache),没有写或执行权限。如果您希望对抗黑客,则可以放松一些限制。不存储盐只会使生活更加困难;他们仍然必须正确获取输入到算法的数据 - 但为什么要让它变得更容易呢?
2b) 如果不存储哈希,则VARCHAR(32)应该对blowfish没问题。
3) 假设您已经运行了适当的注入预防代码等等... 所以请不要盲目复制以下内容(最好使用PDO而不是mysql扩展)。以下内容特定于blowfish、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
}