Skip to content

Commit a3d1020

Browse files
author
lwhhhh
committed
Merge branch 'master' of https://github.com/lwhhhh/python
2 parents c179ce9 + aa0530e commit a3d1020

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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)