密码安全散列一次又一次或只是一次



使用存储密码更安全吗

$password=hash(sha512, $_POST['password']);
$password=hash(sha512, $password);
$password=hash(sha256, $password);
$password=md5('JIOA#324FKJ///#dfr3inj1HJ4_jbbf-vbd31ds4_-_nURl//&dffve@.add123456789fs-@drgko489d', $password);
$password=hash(sha512, $password);

所有这些会比仅仅更安全吗

$password=hash(sha512, $_POST['password']);

否,因为正如towr所指出的,第一个每次都返回相同的$password,这显然不是您想要的。第二个是为了实用,就像把密码保存在明文中一样糟糕。网上有巨大的数据库,你可以在那里查找特定哈希的密码。如果我们看看第一个代码打算做什么(我猜md5步骤是为了连接两个字符串),那就更好了,因为它在md5步骤中使用字符串作为salt。不过salt是静态的,所以它总体上仍然是一个非常糟糕的密码哈希方案。你可能想了解一些密码哈希的基础知识。

无论如何,我建议您遵循ceejayoz的建议,使用PHP 5.5的password_hash函数。

建议的哈希方案都不适合哈希密码,因为它们都太快了。您需要一个慢密钥派生函数,如BCrypt或PBKDF2,具有可调整的成本因子。

PHP提供了新的函数password_hash来计算BCrypt散列。它简化了所有棘手的事情,比如生成安全的盐。对于早期的PHP版本,您可以使用兼容性包。

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// 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);

相关内容

最新更新