Created
October 9, 2013 03:28
-
-
Save olsososo/6895759 to your computer and use it in GitHub Desktop.
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 os | |
from hashlib import sha256 | |
from hmac import HMAC | |
def encrypt_password(password, salt=None): | |
"""Hash password on the fly.""" | |
if salt is None: | |
salt = os.urandom(8) # 64 bits. | |
assert 8 == len(salt) | |
assert isinstance(salt, str) | |
if isinstance(password, unicode): | |
password = password.encode('UTF-8') | |
assert isinstance(password, str) | |
result = password | |
for i in xrange(10): | |
result = HMAC(result, salt, sha256).digest() | |
return salt + result | |
hashed = encrypt_password('secret password') | |
def validate_password(hashed, input_password): | |
return hashed == encrypt_password(input_password, salt=hashed[:8]) | |
assert validate_password(hashed, 'secret password') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment