-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto.py
More file actions
25 lines (22 loc) · 720 Bytes
/
crypto.py
File metadata and controls
25 lines (22 loc) · 720 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
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
import os
def derive_key(master_password: str, salt: bytes) -> bytes:
kdf = Scrypt(
salt=salt,
length=32,
n=2**14,
r=8,
p=1,
)
return kdf.derive(master_password.encode())
def encrypt(secret: str, key: bytes) -> bytes:
aesgcm = AESGCM(key)
nonce = os.urandom(12)
ciphertext = aesgcm.encrypt(nonce, secret.encode(), None)
return nonce + ciphertext
def decrypt(blob: bytes, key: bytes) -> str:
nonce = blob[:12]
ciphertext = blob[12:]
aesgcm = AESGCM(key)
return aesgcm.decrypt(nonce, ciphertext, None).decode()