Skip to content

Commit c8d9d4c

Browse files
committed
add 0021
1 parent 7352023 commit c8d9d4c

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Jaccorot/0021/0021.python

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/python
2+
# coding=utf-8
3+
__author__ = 'Jaccorot'
4+
5+
import os
6+
from hashlib import sha256
7+
from hmac import HMAC
8+
9+
def encrypt_password(password, salt=None):
10+
if salt is None:
11+
salt = os.urandom(8)
12+
assert 8 == len(salt)
13+
assert isinstance(salt, str)
14+
15+
if isinstance(password, unicode):
16+
password = password.encode('utf-8')
17+
assert isinstance(password, str)
18+
19+
for i in range(10):
20+
encrypted = HMAC(password, salt, sha256).digest()
21+
return salt + encrypted
22+
23+
24+
def validate_password(hashed, password):
25+
return hashed == encrypt_password(password, hashed[:8])
26+
27+
28+
if __name__ == "__main__":
29+
password_new = raw_input("Set your password\n")
30+
password_saved = encrypt_password(password_new)
31+
password_again = raw_input("Now,type in your password\n")
32+
print "Yes,you got it." if validate_password(password_saved, password_again) else "No,it's wrong."

0 commit comments

Comments
 (0)