简体   繁体   English

Python md5密码值

[英]Python md5 password value

I have this change password request form.In which the user enter their oldpasswords. 我有这个更改密码请求表单。用户输入他们的旧密码。

this oldpassword is the md5 format. 这个oldpassword是md5格式。

How to compare the md5 value from db to the oldpassword entered by user 如何将db中的md5值与用户输入的oldpassword进行比较

 import md5

 oldpasswd_byuser=str("tom")
 oldpasswd_db="sha1$c60da$1835a9c3ccb1cc436ccaa577679b5d0321234c6f"
 opw=     md5.new(oldpasswd_byuser)
 #opw=     md5.new(oldpasswd_byuser).hexdigest()
 if(opw ==      oldpasswd_db):
    print "same password"
 else:
     print "Invalid password" 

the hash you put in there is a salted sha1 hexdigest as django (and probably many others) stores it by default. 你放在那里的哈希是一个salted sha1 hexdigest,因为django(可能还有很多其他的)默认存储它。

the code to verify it is in contrib/auth/models.py . 验证它的代码在contrib / auth / models.py中 From there you can see that django works with md5 by default. 从那里你可以看到django默认使用md5。 All you have to do is to update the old hashes to the following form: 您所要做的就是将旧哈希更新为以下格式:

md5$<salt>$<hash>

if your hashes aren't salted yet leave the salt empty ( md5$$<hash> ), but update the hash to sha1 the next time the user performs a valid login. 如果您的哈希值没有被加盐,但将盐留空( md5$$<hash> ),但在下次用户执行有效登录时将哈希值更新为sha1。

I don't think that oldpasswd_db is a MD5. 我认为oldpasswd_db不是MD5。 It more looks like a combination of hash method (SHA1 in this case), a salt and the password hash itself. 它看起来更像哈希方法(在本例中为SHA1),salt和密码哈希本身的组合。

Try to concatenate the salt value with the password: 尝试将salt值与密码连接:

import hashlib
hashlib.sha1('c60datom').hexdigest()

It's not md5, it's sha1 - "sha1$xxx . 它不是md5,它是sha1 - "sha1$xxx

You'd have to use sha1 functions instead. 你必须使用sha1函数。 There is a documentation on this at http://docs.python.org/library/sha.html http://docs.python.org/library/sha.html上有关于此的文档

to compare the value of your current password to the password stored in the database you can do: 要比较当前密码的值与数据库中存储的密码,您可以执行以下操作:

import md5

input_password = request.POST['password']
md5_hashed_input_password = md5.new(input_password).hexdigest()
#comapre the value to that stored in db
if md5_hashed_input_password == db_password:  #password in db should be stored in md5 hash format
    print 'password match'
else:
    print 'password mismatch'

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

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