如何在 MySQL 的 `encrypt` 函数中使用 `bcrypt` 算法来验证密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20295778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:35:20  来源:igfitidea点击:

How to use `bcrypt` algorithm within `encrypt` function in MySQL for verifying password?

mysqlencryptionbcrypt

提问by sectus

I have bcrypted value($2y$10$zQaDT8hXM4pLmBdwN0xEseda/oKJAQKMKMzUrV8jbs6Epz28BXzBS) of password (qwe). But when I am verifying I am getting wrong result hash value.

我有$2y$10$zQaDT8hXM4pLmBdwN0xEseda/oKJAQKMKMzUrV8jbs6Epz28BXzBS密码 ( qwe) 的bcrypted 值( )。但是当我验证时,我得到了错误的结果哈希值。

mysql> select 'y$zQaDT8hXM4pLmBdwN0xEseda/oKJAQKMKMzUrV8jbs6Epz28BXzBS' = encrypt('qwe', 'y$zQaDT8hXM4pLmBdwN0xEseda/oKJAQKMKMzUrV8jbs6Epz28BXzBS') as is_valid; 
+----------+
| is_valid |
+----------+
|        0 |
+----------+

select encrypt('qwe', 'y$zQaDT8hXM4pLmBdwN0xEseda/oKJAQKMKMzUrV8jbs6Epz28BXzBS') as hash;
+---------------+
| hash          |
+---------------+
| tBKnsbV2Szg |
+---------------+

md5works fine

md5工作正常

mysql> select '$$.dCRcHz4ApIYzcA0g/qz3/' = encrypt('qwe', '$$.dCRcHz4ApIYzcA0g/qz3/') as is_valid; 
+----------+
| is_valid |
+----------+
|        1 |
+----------+

How to add support of bcryptto MySQL?

如何添加bcrypt对 MySQL 的支持?

回答by duskwuff -inactive-

You can't. The MySQL ENCRYPT()function uses the operating system's crypt()function — if your operating system does not support bcrypt hashes, MySQL will not support them either.

你不能。MySQLENCRYPT()函数使用操作系统的crypt()函数——如果你的操作系统不支持 bcrypt 哈希,MySQL 也不支持它们。

Also, do not use the MySQL ENCRYPT()function. As ircmaxell noted, any data you pass to a MySQL query may end up in server log files, so it's potentially unsafe to use it for anything password-related.

另外,不要使用 MySQLENCRYPT()函数。正如 ircmaxell 所指出的,您传递给 MySQL 查询的任何数据都可能最终出现在服务器日志文件中,因此将其用于任何与密码相关的事情可能是不安全的。