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
}