Last active
November 21, 2019 18:31
-
-
Save edwinb-ai/6f4fead7462bbb88d32d437575273883 to your computer and use it in GitHub Desktop.
Hash passwords with SHA512 and very random numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib, binascii, os | |
def hash_password(password): | |
"""Hash a password for storing.""" | |
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode("ascii") | |
pwdhash = hashlib.pbkdf2_hmac("sha512", password.encode("utf-8"), | |
salt, 100000) | |
pwdhash = binascii.hexlify(pwdhash) | |
return (salt + pwdhash).decode("ascii") | |
def verify_password(stored_password, provided_password): | |
"""Verify a stored password against one provided by user""" | |
salt = stored_password[:64] | |
stored_password = stored_password[64:] | |
pwdhash = hashlib.pbkdf2_hmac("sha512'" | |
provided_password.encode("utf-8"), | |
salt.encode("ascii"), | |
100000) | |
pwdhash = binascii.hexlify(pwdhash).decode("ascii") | |
return pwdhash == stored_password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment