Skip to content

Instantly share code, notes, and snippets.

@mrnejc
Created November 16, 2013 19:19
Show Gist options
  • Save mrnejc/7504118 to your computer and use it in GitHub Desktop.
Save mrnejc/7504118 to your computer and use it in GitHub Desktop.
python - create password hash / test password against hash
import uuid
import hashlib
def hash_password(password, version=1, salt=None):
if version == 1:
if salt == None:
salt = uuid.uuid4().hex[:16]
hashed = salt + hashlib.sha1( salt + password).hexdigest()
# generated hash is 56 chars long
return hashed
# incorrect version ?
return None
def test_password(password, hashed, version=1):
if version == 1:
salt = hashed[:16]
rehashed = hash_password(password, version, salt)
return rehashed == hashed
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment