Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying a PBKDF2 password hash in python-pbkdf2

I am using the snippet below to encrypt user password before saving in the database.

from pbkdf2 import crypt
pwhash = crypt(password_from_user)

Example: $p5k2$$Y0qfZ64u$A/pYO.3Mt9HstUtEEhWH/RXBg16EXDMr

Then, I save this in database. Well locally, I can perform a check doing something like this:

from pbkdf2 import crypt
  pwhash = crypt("secret")
  alleged_pw = raw_input("Enter password: ")
  if pwhash == crypt(alleged_pw, pwhash):
      print "Password good"
  else:
      print "Invalid password"

but how do I perform checks with what is on the db as the encrypted string is not always the same. I'm using python-pbkdf2.

like image 593
Nosakhare Belvi Avatar asked Oct 20 '25 09:10

Nosakhare Belvi


1 Answers

Okey, Did more research and figured out that to achieve this, i first have to encrypt the password and save in db.as:

pwhash = crypt("secret",iterations=1000)

which can produce a string like $p5k2$3e8$her4h.6b$.p.OE5Gy4Nfgue4D5OKiEVWdvbxBovxm

and to validate when a user wants to login with same password, i use the function below:

def isValidPassword(userPassword,hashKeyInDB):
     result = crypt(userPassword,hashKeyInDB,iterations = 1000)
     return reesult == hashKeyInDB #hashKeyInDB in this case is $p5k2$3e8$her4h.6b$.p.OE5Gy4Nfgue4D5OKiEVWdvbxBovxm

this method returns True if the password is same or False if otherwise.

like image 147
Nosakhare Belvi Avatar answered Oct 21 '25 21:10

Nosakhare Belvi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!