The most common ( not secure ) hash algorithm is md5 . Extracting a few ideas from a Django password system might be as follows:
import hashlib def make_password(password): assert password hash = hashlib.md5(password).hexdigest() return hash def check_password(hash, password): """Generates the hash for a password and compares it.""" generated_hash = make_password(password) return hash == generated_hash >>> hash = make_password('hello123') >>> hash 'f30aa7a662c728b7407c54ae6bfd27d1' >>> check_password(hash, 'hello123') True >>> check_password(hash, 'Hello123') False
Use make_password to generate the hash and check_password to check if the entered password matches the saved one.
As @Emil noted, Django supports multiple passwords, such as pbkdf2_sha256 and pbkdf2_sha1, storing the string as a 3x value, separated by $ : algorithm$salt$hash . salt is a randomly generated string to prevent the same password from being repeated in the database.
source share