Sco*_*ski 17
顺便提一下:如何在2016年安全地存储用户密码.
你的选择是:
如果你真的需要,也可以随意考虑PBKDF2.
鉴于您是初学者,您应该像这样编写密码验证:
// Creating your hashed password:
$hash = password_hash($userPassword, PASSWORD_DEFAULT);
// Checking a user-supplied password against a stored hash:
if (password_verify($userPassword, $hash)) {
// Login successful.
if (password_needs_rehash($hash, PASSWORD_DEFAULT)) {
// Recalculate a new password_hash() and overwrite the one we stored previously
}
}
Run Code Online (Sandbox Code Playgroud)
下行到bcrypt:
密码锁中内置了一个解决这些限制的障碍:它使用SHA384对密码进行预先散列,然后在传递给PHP的密码API之前对原始散列进行base64编码.
首先,创建加密密钥并将其存储在文档根目录之外.(否则,黑客可以窃取密钥.)
$newKey = \Defuse\Crypto\Key::createNewRandomKey();
file_put_contents(
'/outside/document/root/enckey.txt',
$newKey->saveToAsciiSafeString()
);
Run Code Online (Sandbox Code Playgroud)
现在,您可以将此密钥与您的密码结合使用:
$key = Key::loadFromAsciiSafeString(
file_get_contents('/outside/document/root/enckey.txt')
);
// Hashing a password with PasswordLock:
$storeMe = PasswordLock::hashAndEncrypt($_POST['password'], $key);
// Verifying a password with PasswordLock:
if (PasswordLock::decryptAndVerify($_POST['password'], $storeMe, $key)) {
// Success!
}
Run Code Online (Sandbox Code Playgroud)
password_hash()在PHP 7.2中使用Argon2除非你使用的是PHP 7.2或更高版本,否则你需要安装libsodium和PHP扩展来使用Argon2.密码哈希是sodium_compat 未提供的功能之一.
// Password hashing:
$hash_str = sodium_crypto_pwhash_str(
$password,
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
// Password verification:
if (sodium_crypto_pwhash_str_verify($hash_str, $password)) {
// recommended: wipe the plaintext password from memory
sodium_memzero($password);
// Password was valid.
} else {
// recommended: wipe the plaintext password from memory
sodium_memzero($password);
// Password was invalid.
}
Run Code Online (Sandbox Code Playgroud)
您需要通过PECL提供的scrypt扩展:
pecl install scrypt
echo "extension=scrypt.so" > /etc/php5/mods-available/scrypt.ini
php5enmod scrypt
Run Code Online (Sandbox Code Playgroud)
一旦安装,使用它是相当简单的:
// Hashing:
$hash = \Password::hash($userProvidedPassword);
// Validation:
if (\Password::check($userProvidedPassword, $hash)) {
// Logged in successfully.
}
Run Code Online (Sandbox Code Playgroud)
真正使用scrypt的唯一原因是兼容性; 此时,请使用Argon2或bcrypt.
如果您需要PBKDF2,我强烈建议您使用Defuse Security的跨平台密码哈希库.(你应该考虑使用password_*,但是!)
$hash = PasswordStorage::create_hash($password);
if (PasswordStorage::verify_password($password, $hash)) {
// Success
}
Run Code Online (Sandbox Code Playgroud)
任何上述选择都是可以接受的.Argon2可能是最安全的,但它尚未在PHP中广泛使用.此列表中缺少的任何内容都应被视为怀有健康的怀疑态度.
重要的是,该算法提供了一个成本因子,它控制计算哈希的必要时间.您可以投入更多时间来计算单个哈希值,将会产生更昂贵的暴力破解(例如,每秒100 Giga MD5与每秒10 BCrypt相比).
今天推荐的算法是BCrypt,PBKDF2和SCrypt.PHP支持算法BCrypt,包装函数负责生成盐,是未来的证明.
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($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($password, $existingHashFromDb);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6329 次 |
| 最近记录: |