tags:

views:

43

answers:

3

I have this change password request form.In which the user enter their oldpasswords.

this oldpassword is the md5 format.

How to compare the md5 value from db to the oldpassword entered by user

 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" 
+1  A: 

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

You'd have to use sha1 functions instead. There is a documentation on this at http://docs.python.org/library/sha.html

sukru
+2  A: 

I don't think that oldpasswd_db is a MD5. It more looks like a combination of hash method (SHA1 in this case), a salt and the password hash itself.

Try to concatenate the salt value with the password:

import hashlib
hashlib.sha1('c60datom').hexdigest()
olt
+1  A: 

Hi,

the hash you put in there is a salted sha1 hexdigest as django (and probably many others) stores it by default.

the code to verify it is in contrib/auth/models.py. From there you can see that django works with md5 by default. 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.

Till Backhaus
@Till Backhaus:Could u give a relevant example for converting the string tom to md5 as per ur explaination
Rajeev
Salting (http://en.wikipedia.org/wiki/Salt_%28cryptography%29) passwords is done to prevent dictionary attacks against your database. I'd wrap the login view as described in a question about login signals (http://stackoverflow.com/questions/1990502/django-signal-when-user-logs-in) and update the hash in the wrapped view. So you don't hash the string `'torn'` yourself, but you call `user.set_password('torn')` from the login view wrapper if `user.is_authenticated() and user.password.startswith('md5$$')`.
Till Backhaus