This code snippet implements password hashing for safe storage. It uses Argon2 as hashing algorithm. The method
hash_password() is used to hash and salt a password. The method
verify() is used to check whether a password matches a hash.
pip install argon2-cffi
from argon2 import PasswordHasher from argon2.exceptions import VerifyMismatchError def hash_password(password): ph = PasswordHasher() pw_hash = ph.hash(password) return pw_hash def verify(pw_hash, password): ph = PasswordHasher() try: return ph.verify(pw_hash, password) except VerifyMismatchError: return False def main(): pw_hash = hash_password('s3cr3t') print(pw_hash) print(verify(pw_hash, 's3cr3t')) print(verify(pw_hash, 's3cr4t')) return None if __name__ == '__main__': main()
1- Do you think the implementation of hash_password is up-to-date and secure?
2- Do you think the implementation of verify is up-to-date and secure?
3- Would you make any other changes to this code snippet?
4- Did you use any additional resources while checking the code? If yes, please provide a link or description of your resource