forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0021.python
More file actions
32 lines (24 loc) · 902 Bytes
/
0021.python
File metadata and controls
32 lines (24 loc) · 902 Bytes
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
26
27
28
29
30
31
32
#!/usr/bin/python
# coding=utf-8
__author__ = 'Jaccorot'
import os
from hashlib import sha256
from hmac import HMAC
def encrypt_password(password, salt=None):
if salt is None:
salt = os.urandom(8)
assert 8 == len(salt)
assert isinstance(salt, str)
if isinstance(password, unicode):
password = password.encode('utf-8')
assert isinstance(password, str)
for i in range(10):
encrypted = HMAC(password, salt, sha256).digest()
return salt + encrypted
def validate_password(hashed, password):
return hashed == encrypt_password(password, hashed[:8])
if __name__ == "__main__":
password_new = raw_input("Set your password\n")
password_saved = encrypt_password(password_new)
password_again = raw_input("Now,type in your password\n")
print "Yes,you got it." if validate_password(password_saved, password_again) else "No,it's wrong."