-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption_h33.py
More file actions
28 lines (24 loc) · 958 Bytes
/
encryption_h33.py
File metadata and controls
28 lines (24 loc) · 958 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
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.fernet import Fernet
import os
class H33Encryption:
def __init__(self, password: str, salt: bytes = None):
self.salt = salt or os.urandom(16)
self.key = self.generate_key(password)
def generate_key(self, password: str) -> bytes:
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=self.salt,
iterations=100000,
backend=default_backend()
)
return base64.urlsafe_b64encode(kdf.derive(password.encode()))
def encrypt(self, data: str) -> bytes:
fernet = Fernet(self.key)
return fernet.encrypt(data.encode())
def decrypt(self, token: bytes) -> str:
fernet = Fernet(self.key)
return fernet.decrypt(token).decode()