简体   繁体   English

如何在PHP中将密码以BCRYPT的形式存储为数据库中的$ 2a $ 15

[英]How to store a password as BCRYPT in a database as '$2a$15' in PHP

I am wondering on how to store passwords as BCRYPT that starts with $2a$15 , because I have already setup my register and it already stores the passwords in BCRYPT . 我想知道如何将密码存储为以$2a$15开头的BCRYPT ,因为我已经设置了寄存器,并且已经将密码存储在BCRYPT

However, when it stores the passwords as BCRYPT , the output is $2y$15 , but I want it as $2a$15 . 但是,当它将密码存储为BCRYPT ,输出为$2y$15 ,但我希望它为$2a$15 I am new to BCRYPT and I find it quite hard to understand. 我是BCRYPT ,我很难理解。

Anyway, here is my BCRYPT code: 无论如何,这是我的BCRYPT代码:

function encryptedPassword($strPassword) {
    $strSHA256 = hash('sha256', $strPassword);
    $strBcrypt = password_hash($strSHA256, PASSWORD_BCRYPT, array('cost' => 15, 'salt' => bin2hex(openssl_random_pseudo_bytes(12))));
    return $strBcrypt;
}

So, say if my password is Hello123! 所以,说我的密码是否为Hello123! . The output of that would be: 输出为:

$2y$15$aa979703a103b0a45b2afO0KS15RjWu5Lc8sa4.xQlsw9QLtyhp/O

and I want it to be: 我希望它是:

$2a$15$aa979703a103b0a45b2afO0KS15RjWu5Lc8sa4.xQlsw9QLtyhp/O

I don't know if this is to do with the salt, can someone help me? 我不知道这是否与食盐有关,有人可以帮我吗?

From PHP manual: 从PHP手册:

PASSWORD_BCRYPT - Use the CRYPT_BLOWFISH algorithm to create the hash. PASSWORD_BCRYPT-使用CRYPT_BLOWFISH算法创建哈希。 This will produce a standard crypt() compatible hash using the "$2y$" identifier. 这将使用“ $ 2y $”标识符生成标准的crypt()兼容哈希。 The result will always be a 60 character string, or FALSE on failure. 结果将始终为60个字符串,否则为FALSE。

So using BCRYPT will always produce $2y$. 因此,使用BCRYPT将始终产生$ 2y $。 By the way, the begining part of the hash is just an identifier, used for testing the password afterwards with password_verify. 顺便说一句,哈希的开头部分只是一个标识符,用于随后使用password_verify测试密码。 Just like the $15$ in your case being the cost of the hashing algorithm... 就像您的情况中的$ 15 $是哈希算法的成本...

I suggest you read more on PHP password hashing. 我建议您阅读有关PHP密码哈希的更多信息。 There is absolutely no sane reason why you'd want a hash to absolutely start with X or Y... 绝对没有理智的理由为什么您希望哈希绝对以X或Y开头...

The PHP folks have put great efforts on giving you secure password hashing functions. PHP的人们一直在努力为您提供安全的密码哈希函数。 You should really try using them without hacking stuff around it. 您应该真正尝试使用它们,而不用乱砍东西。 hashing a hash is no safer than just hashing once. 哈希哈希比哈希一次更安全。 The salt option is deprecated in PHP7, I'd suggest you get rid of it, PHP can handle it better than you. salt选项在PHP7中已被弃用,建议您摆脱它,PHP比您可以更好地处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM