2016-05-18 17:12:46 +00:00
|
|
|
from helpers import cryptHelper
|
2016-04-19 17:40:59 +00:00
|
|
|
import base64
|
|
|
|
import bcrypt
|
|
|
|
|
|
|
|
def checkOldPassword(password, salt, rightPassword):
|
|
|
|
"""
|
|
|
|
Check if password+salt corresponds to rightPassword
|
|
|
|
|
|
|
|
password -- input password
|
|
|
|
salt -- password's salt
|
|
|
|
rightPassword -- right password
|
|
|
|
return -- bool
|
|
|
|
"""
|
|
|
|
|
2016-05-18 17:12:46 +00:00
|
|
|
return (rightPassword == cryptHelper.crypt(password, "$2y$"+str(base64.b64decode(salt))))
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
def checkNewPassword(password, dbPassword):
|
|
|
|
"""
|
|
|
|
Check if a password (version 2) is right.
|
2016-05-18 17:12:46 +00:00
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
password -- input password
|
|
|
|
dbPassword -- the password in the database
|
|
|
|
return -- bool
|
|
|
|
"""
|
|
|
|
password = password.encode("utf8")
|
2016-05-18 17:12:46 +00:00
|
|
|
dbPassword = dbPassword.encode("utf8")
|
2016-04-19 17:40:59 +00:00
|
|
|
return bcrypt.hashpw(password, dbPassword) == dbPassword
|
|
|
|
|
|
|
|
def genBcrypt(password):
|
|
|
|
"""
|
|
|
|
Bcrypts a password.
|
2016-05-18 17:12:46 +00:00
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
password -- the password to hash.
|
|
|
|
return -- bytestring
|
|
|
|
"""
|
|
|
|
return bcrypt.hashpw(password.encode("utf8"), bcrypt.gensalt(10, b'2a'))
|