gpt4 book ai didi

php - 关于PHP/MySQL密码安全的问题

转载 作者:行者123 更新时间:2023-11-29 05:10:06 25 4
gpt4 key购买 nike

function password_encrypt($password) {
$hash_format = "$2y$10$"; // Tells PHP to use Blowfish with a "cost" of 10
$salt_length = 22; // Blowfish salts should be 22-characters or more
$salt = generate_salt($salt_length);
$format_and_salt = $hash_format . $salt;
$hash = crypt($password, $format_and_salt);
return $hash;
}

function generate_salt($length) {
// Not 100% unique, not 100% random, but good enough for a salt
// MD5 returns 32 characters
$unique_random_string = md5(uniqid(mt_rand(), true));
// Valid characters for a salt are [a-zA-Z0-9./]
$base64_string = base64_encode($unique_random_string);
// But not '+' which is valid in base64 encoding
$modified_base64_string = str_replace('+', '.', $base64_string);
// Truncate string to the correct length
$salt = substr($modified_base64_string, 0, $length);
return $salt;
}

你们认为这安全吗?可以做些什么?什么可能更容易用来保护密码并对其进行哈希处理?

最佳答案

Blowfish 本身已经非常安全。一件事:不要做太多哈希等来生成盐。另外,为什么不使用 password_hash 让它变得更简单呢?

http://php.net/manual/en/function.password-hash.php

例子:

echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT)."\n";

检查密码:

if (password_verify($password_nonhashed, $password_hashed)) {

你不需要在河豚身上撒太多盐。如果您真的不想使用 password_hash,只需使用 sha1 散列作为盐。

祝你好运!

关于php - 关于PHP/MySQL密码安全的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40861627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com