如何在 MySQL 中散列密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/704194/
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
How to hash passwords in MySQL?
提问by santanu
How I will make every password in my user table encrypted(md5()) except one particular row using a single query?
除了使用单个查询的特定行之外,如何使用户表中的每个密码都加密(md5())?
回答by jerebear
UPDATE table SET Password = MD5(Password)
I will say though that MD5 isn't a very good level of encryption and you should consider something stronger such as ENCRYPT with a custom salt. Read about it here
我会说,尽管 MD5 不是一个很好的加密级别,您应该考虑使用更强大的东西,例如带有自定义盐的 ENCRYPT。在这里阅读
EDIT: Looks like the original question changed. Here's the altered query to accomodate
编辑:看起来原来的问题改变了。这是更改后的查询以适应
UPDATE table SET Password = MD5(Password) WHERE ID!=[specified index]
EDIT: Worth noting
编辑:值得注意
回答by guerda
Hash Functions in MySQL
MySQL 中的哈希函数
There are a lot more hash functions than MD5 to use for storing passwords in you MySQL database.
You can find a list of them on MySQL :: 11.10.2. Encryption and Compression Functions.
用于在 MySQL 数据库中存储密码的哈希函数比 MD5 多得多。
您可以在MySQL :: 11.10.2上找到它们的列表。加密和压缩功能。
Save Password (hash):
保存密码(哈希):
UPDATE users SET password = SHA('secret_password') WHERE ....;
Check Password:
检查密码:
SELECT COUNT(*) FROM users WHERE name = 'username' && password = SHA('typed_password');
If the result is > 0, the user provided the correct password.
如果结果 > 0,则用户提供了正确的密码。
回答by Quassnoi
When hashing passwords, do not forget to salt them, so that same passwords do not yield same hashes:
对密码进行散列时,不要忘记给它们加盐,这样相同的密码就不会产生相同的散列:
SET @salt := CONV(FLOOR(RAND() * 0x100000000), 10, 16)
UPDATE passwords
SET password = CONCAT(@salt, SHA(CONCAT(@salt, @typed_password)))
SELECT 1
FROM passwords
WHERE SHA(CONCAT(SUBSTRING(password, 1, 8), @typed_password)) = SUBSTRING(password, 9, 40)
回答by lc.
Edited in response to edit in OP.
编辑以响应 OP 中的编辑。
UPDATE userTable
SET password = MD5(password)
WHERE NOT (<criteria to identify row to exclude>)
回答by lc.
Concerning you edit: do you have an ID or username that identifies this row?
关于您编辑:您是否有标识此行的 ID 或用户名?
UPDATE mytable
SET password = MD5(password)
WHERE id <> 123
回答by ackuser
I think it is a little bit more update
我觉得更新有点多
SET PASSWORD FOR 'existinguser'@'localhost' = PASSWORD('newpass');
or
或者
UPDATE user SET password = PASSWORD('newpass');
Hope this help
希望这有帮助