这几乎是做到这一点的方法。在大多数情况下,最好省略salt参数。如果没有这个参数,该函数将从操作系统的随机源生成一个加密安全的盐。
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT, array("cost" => 11));
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
有点更详细:你的方法生成盐,将需要超过必要的随机字节,因为BCrypt预计编码盐,不是你创建一个二进制盐。据我所知,函数password_hash会正确编码它,但它只会使用必要的字节。
注意:在这种情况下,您可以像使用$ _POST ['password']一样直接使用(不需要转义),因为散列函数可以处理所有类型的输入并生成一个“安全”字符串,不容易SQL注入。
使用单向加密,如sha1()或md5() – Sundar
只是不直接放置$ _POST。其余的看起来不错imo –
@Sundar - 推荐不安全的散列(甚至没有提到盐渍)当OP询问推荐的解决方案在PHP中的密码处理并不是一个非常明智的建议 –