Skip to content

Commit aa0530e

Browse files
committed
Create encrpyt.py
1 parent 4294bd5 commit aa0530e

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

lwh/21/encrpyt.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from hashlib import sha256
2+
from hmac import HMAC
3+
import os
4+
5+
6+
class Encrypt(object):
7+
8+
def encrypt(self, password, salt=None):
9+
if salt is None:
10+
salt = os.urandom(8)
11+
result = password.encode('utf-8')
12+
for i in range(10):
13+
result = HMAC(result, salt, sha256).digest()
14+
return salt + result
15+
16+
def vaildate(self, password, hashed):
17+
return hashed == self.encrypt(password, salt=hashed[:8])
18+
19+
if __name__ == '__main__':
20+
obj = Encrypt()
21+
hashed = obj.encrypt('wh5622')
22+
# print(bytes.decode(hashed))
23+
ans = obj.vaildate('wh5622', hashed)
24+
print(ans)

0 commit comments

Comments
 (0)