-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto_utils.py
More file actions
29 lines (26 loc) · 912 Bytes
/
Copy pathcrypto_utils.py
File metadata and controls
29 lines (26 loc) · 912 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
# Codes By Visionnn
import os
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def derive_key(password, salt):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
return kdf.derive(password.encode())
def encrypt_data(data, password):
salt = os.urandom(16)
key = derive_key(password, salt)
aesgcm = AESGCM(key)
nonce = os.urandom(12)
ciphertext = aesgcm.encrypt(nonce, data, None)
return salt, nonce, ciphertext
def decrypt_data(encrypted_data, password, salt, nonce):
key = derive_key(password, salt)
aesgcm = AESGCM(key)
return aesgcm.decrypt(nonce, encrypted_data, None)