-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.py
36 lines (31 loc) · 1.08 KB
/
security.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 bcrypt
from passlib.context import CryptContext
import hashlib
import os
# Set up password context
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def hash_password(password: str) -> str:
"""
Hash a password for storing.
"""
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""
Verify a stored password against one provided by user
"""
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
"""
Generate a new hashed password
"""
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
return salt.hex() + key.hex()
def verify_password(plain_password: str, stored_password: str) -> bool:
"""
Verify a stored password against one provided by user
"""
salt = bytes.fromhex(stored_password[:64])
stored_key = stored_password[64:]
new_key = hashlib.pbkdf2_hmac('sha256', plain_password.encode('utf-8'), salt, 100000).hex()
return new_key == stored_key