-
Notifications
You must be signed in to change notification settings - Fork 1
/
password_service.py
36 lines (33 loc) · 1.26 KB
/
password_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import binascii
import hashlib
import os
class PasswordService:
"""
Provides methods to hash a password and to verify if
password hash corresponds to the provided password.
"""
@staticmethod
def hash_password(password) -> str:
"""
Hash provided password.
:type password: str
"""
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pass_hash = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'),
salt, 100000)
pass_hash = binascii.hexlify(pass_hash)
return (salt + pass_hash).decode('ascii')
@staticmethod
def verify_password(hashed_password, password) -> bool:
"""
Verify previously hashed password to match the provided one
:type password: str
:type hashed_password: str
"""
salt = hashed_password[:64]
hashed_password = hashed_password[64:]
pass_hash = hashlib.pbkdf2_hmac('sha512',
password.encode('utf-8'),
salt.encode('ascii'), 100000)
pass_hash = binascii.hexlify(pass_hash).decode('ascii')
return pass_hash == hashed_password