def check_secure_val(h): """Check a secure string with hash.""" val, hashed = h.split('|') try: if bcrypt.hashpw(val, hashed) == hashed: return val except ValueError: return None
def encrypt_password(password): """Encrypts (bcrypt) a given password. Args: password (str): The password to encrypt. Returns: str: The encrypted password. """ return bcrypt.hashpw(password, bcrypt.gensalt())
def compare_passwords(stored_pw, unencrypted_pw): """Compares a stored password with an unencrypted password to see if they match. Args: stored_pw (str): The password that is currently stored in the DB. unencrypted_pw (str): An unencrypted password to compare to the stored password. Returns: bool: Returns True if the passwords match. False, otherwise. """ return bcrypt.hashpw(unencrypted_pw, stored_pw) == stored_pw
def check_username_password(cls, username, password): """Chech a secure hash of a password against a user using bcrypt.""" thisUser = UserEntity.by_name(username) if thisUser and bcrypt.hashpw(password, thisUser.password) == thisUser.password: return thisUser
def hash_password(cls, password): """Create a secure hash of password using bcrypt""" return bcrypt.hashpw(password, bcrypt.gensalt())
def hash_str(s): """Hash a string, for security.""" return bcrypt.hashpw(s, bcrypt.gensalt())