将数据库中的文本密码转换为哈希密码?
在我的数据库中有超过 600 个用户。密码以前以纯文本形式存储(我知道,这很简单)。无论如何,我已经更改了代码来存储 STA1 哈希密码,但我需要转换数据库中的现有密码,这样每个用户就不需要进入并修改他们的帐户。
有什么帮助吗?
In my database I have over 600 users. The passwords were previously stored as plain text (slap on hand, I know). Anyways, I have changed my code to store STA1 hashed passwords, but I need to convert the existing passwords in my database so each user doesn't need to go in and modify their account.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,使用 mysqldump 备份数据库。例如
,此外,请确保您的密码字段的长度为 40 个字符。如果没有,请执行此 SQL 命令:
然后执行此命令来更改密码:
First, backup your database with mysqldump. For example
Also, make sure that your password field is 40 characters long. If not, execute this SQL command:
and then this to change the passwords:
试试这个:
在执行此操作之前进行适当的备份,因为您将无法反转该操作(这就是哈希函数的全部目的)。
请记住,SHA1 将返回二进制值的十六进制编码。
Try this:
Make a proper backup before doing this, since you won't be able to reverse the operation (that's the whole purpose of a HASH function).
Keep in mind that SHA1 will return HEX encoding of the binary value.
如果您的数据库中已经有用户使用散列密码,那么在一次替换所有表时要小心。您最终可能会得到再次转换为新的 SHA1 哈希值的 SHA1 哈希值。
如果您已经有使用散列密码的新用户,请编写一个脚本来查询数据库中的所有密码,如果它们的长度小于 40(如果将第二个参数设置为 true,则为 20),然后根据以下内容生成新的 SHA1 散列当前密码(如果少于 40 个字符,您就会知道它是明文密码)并用新密码替换旧密码。
If you already have users in your database with hashed passwords, then be careful about replacing the table all at once. You may end up with SHA1 hashes that got converted again to a new SHA1 hash.
If you already have new users with hashed passwords, write a script that queries the database for all passwords, if their length is less than 40 (or 20 if you're setting the second parameter to true) then generate a new SHA1 hash based on the current password (which you'll know is plaintext if it is less than 40 characters) and replace the old password with the new one.