-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_key.py
More file actions
28 lines (22 loc) · 812 Bytes
/
generate_key.py
File metadata and controls
28 lines (22 loc) · 812 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
import base64
import os
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
#generates a key accepted for fernet from a given password
#receives string:password optional b:salt
#returns b:key b:salt to be used/stored.
def generate_key(password, salt=os.urandom(16)):
#parsing password to bytes
password = bytes(password, 'ascii')
#generating hash with salt to resist rainbow tables attack
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=480000,
)
key = base64.urlsafe_b64encode(kdf.derive(password))
return key, salt
key, salt = generate_key("wsad8546", b'vWANQGcU3mn_KaWQbeIcKEA2J1sifMIgvpnk0GqS_gI=')
print(key)