如何将密码哈希从MD5转换为SHA?

编程入门 行业动态 更新时间:2024-10-27 04:26:05
本文介绍了如何将密码哈希从MD5转换为SHA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个旧的应用程序,使用MD5散列将用户密码存储在数据库中。我想用SHA-2家族中的某些东西来代替它。

我想到了两种可能的方法来完成这个任务,但都显得相当笨拙。 / p>

1)添加一个布尔标志字段。用户第一次在此之后进行身份验证时,请将MD5密码哈希替换为SHA密码哈希,并设置标志。然后我可以检查标志以查看密码散列是否已被转换。

2)添加第二个密码字段以存储SHA散列。用户第一次在此之后进行身份验证时,使用SHA对密码进行散列并将其存储在新字段中(可能同时删除其MD5散列)。然后我可以检查SHA字段是否有值;这基本上就是我的标志。

在任何一种情况下,对于任何不频繁登录的用户,MD5身份验证都必须保留一段时间。任何不再活动的用户都不会切换到SHA。

有没有更好的方法来做到这一点?

解决方案

基本上相同,但可能比添加额外字段更优雅:在 Django ,密码散列存储为如下所示的字符串:

hashtype $ salt $哈希

哈希类型是sha1或md5,salt是一个随机字符串,用于限制原始密码,最后是散列本身。示例值:

sha1 $ a1976 $ a36cc8cbf81742a8fb52e221aaeab48ed7f58ab4

I've got an old application that has user passwords stored in the database with an MD5 hash. I'd like to replace this with something in the SHA-2 family.

I've thought of two possible ways to accomplish this, but both seem rather clunky.

1) Add a boolean "flag" field. The first time the user authenticates after this, replace the MD5 password hash with the SHA password hash, and set the flag. I can then check the flag to see whether the password hash has been converted.

2) Add a second password field to store the SHA hash. The first time the user authenticates after this, hash the password with SHA and store it in the new field (probably delete their MD5 hash at the same time). Then I can check whether the SHA field has a value; this essentially becomes my flag.

In either case, the MD5 authentication would have to remain in place for some time for any users who log in infrequently. And any users who are no longer active will never be switched to SHA.

Is there a better way to do this?

解决方案

Essentially the same, but maybe more elegant than adding extra fields: In the default authentication framwork in Django, the password hashes are stored as strings constructed like this:

hashtype$salt$hash

Hashtype is either sha1 or md5, salt is a random string used to salt the raw password and at last comes the hash itself. Example value:

sha1$a1976$a36cc8cbf81742a8fb52e221aaeab48ed7f58ab4

更多推荐

如何将密码哈希从MD5转换为SHA?

本文发布于:2023-10-24 23:58:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   密码   SHA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!