-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.py
37 lines (31 loc) · 1.06 KB
/
helpers.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
37
def hash_password(password: str) -> str:
"""Hash a password for storing."""
import binascii
import hashlib
import os
salt = b"__hash__" + hashlib.sha256(os.urandom(60)).hexdigest().encode(
"ascii"
)
pwdhash = hashlib.pbkdf2_hmac(
hash_name="sha512",
password=password.encode("utf-8"),
salt=salt,
iterations=100000,
)
pwdhash = binascii.hexlify(data=pwdhash)
return (salt + pwdhash).decode("ascii")
def is_hash(pw: str) -> bool:
return pw.startswith("__hash__") and len(pw) == 200
def verify_password(stored_password: str, provided_password: str) -> bool:
"""Verify a stored password against one provided by user."""
import binascii
import hashlib
salt = stored_password[:72]
stored_password = stored_password[72:]
pwdhash: bytes = hashlib.pbkdf2_hmac(
hash_name="sha512",
password=provided_password.encode("utf-8"),
salt=salt.encode("ascii"),
iterations=100000,
)
return binascii.hexlify(pwdhash).decode("ascii") == stored_password