blender-id/bid_main/hashers.py

31 lines
781 B
Python

"""
Password hasher that performs the same operations as Flask-Security.
"""
import base64
import hashlib
import hmac
import time
from django.contrib.auth.hashers import BCryptPasswordHasher
_password_salt = b"/2aX16zPnnIgfMwkOjGX4S"
def get_hmac(password: str) -> str:
h = hmac.new(_password_salt, password.encode("utf-8"), hashlib.sha512)
return base64.b64encode(h.digest()).decode("ascii")
class BlenderIdPasswordHasher(BCryptPasswordHasher):
algorithm = "blenderid"
def encode(self, password: str, salt: bytes):
hashed_password = get_hmac(password)
return super().encode(hashed_password, salt)
def shortuid() -> str:
"""Generate a 14-characters long string ID based on time."""
return hex(int(time.monotonic() * 10 ** 10))[2:]