mirror of
https://github.com/avatao-content/test-tutorial-framework
synced 2024-12-05 02:01:33 +00:00
Refactor PasswordHasher
This commit is contained in:
parent
4a08687ac8
commit
9faafa7f49
@ -3,17 +3,22 @@ from hashlib import scrypt
|
||||
|
||||
|
||||
class PasswordHasher:
|
||||
@staticmethod
|
||||
def hash(password):
|
||||
salt = urandom(32)
|
||||
return PasswordHasher.scrypt(password, salt).hex()+salt.hex()
|
||||
n = 16384
|
||||
r = 8
|
||||
p = 1
|
||||
dklen = 32
|
||||
|
||||
@staticmethod
|
||||
def verify(password, salted_hash):
|
||||
@classmethod
|
||||
def hash(cls, password):
|
||||
salt = urandom(32)
|
||||
return cls.scrypt(password, salt).hex() + salt.hex()
|
||||
|
||||
@classmethod
|
||||
def verify(cls, password, salted_hash):
|
||||
salt = bytes.fromhex(salted_hash)[32:]
|
||||
hashdigest = bytes.fromhex(salted_hash)[:32]
|
||||
return PasswordHasher.scrypt(password, salt) == hashdigest
|
||||
return cls.scrypt(password, salt) == hashdigest
|
||||
|
||||
@staticmethod
|
||||
def scrypt(password, salt):
|
||||
return scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=32)
|
||||
@classmethod
|
||||
def scrypt(cls, password, salt):
|
||||
return scrypt(password.encode(), salt=salt, n=cls.n, r=cls.r, p=cls.p, dklen=cls.dklen)
|
||||
|
Loading…
Reference in New Issue
Block a user