-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasshandler.py
39 lines (37 loc) · 1.15 KB
/
passhandler.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import base64
import os
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.fernet import Fernet
def hash(dbpassword,data,salt):
#salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(dbpassword)) # Can only use kdf once
f=Fernet(key)
encrypted = f.encrypt(data)
return encrypted
def dehash(dbpassword,encrypted,salt):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(dbpassword))
f=Fernet(key)
while True:
try:
decrypted = f.decrypt(encrypted)
decrypted_decoded = decrypted.decode()
return decrypted_decoded
except:
incorrect_password = "Different password used to encrypt data"
return incorrect_password