匿名用户
是的,您正确理解了它,函数password_hash()将自己生成一个salt,并将其包含在结果的哈希值中。 将salt存储在数据库中是绝对正确的,即使已知,它也能完成它的任务。// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT);
// 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($_POST['password'], $existingHashFromDb);
您提到的第二个salt(存储在文件中的salt)实际上是一个pepper或服务器端密钥。 如果你在碎之前加入它(就像盐),那么你就会加入胡椒粉。 但是有一个更好的方法,您可以首先计算散列,然后用服务器端密钥加密(双向)散列。 这使您有可能在必要时更改密钥。
与盐相反,这把钥匙应该保密。 人们常常把它混在一起,试图把盐藏起来,但最好还是让盐各司其职,用一把钥匙把秘密加进去。