-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy path1.9(py2).py
25 lines (24 loc) · 873 Bytes
/
1.9(py2).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os,base64
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
def validate_password(hashed, input_password):
return hashed == encrypt_password(input_password, salt=hashed[:8])
if __name__=="__main__":
hashed = encrypt_password('secret password')
assert validate_password(hashed, 'secret password')
print (hashed)
print (base64.b64encode(hashed))
print (base64.b64decode(base64.b64encode(hashed)))