-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass_key_gen.py
More file actions
27 lines (25 loc) · 809 Bytes
/
pass_key_gen.py
File metadata and controls
27 lines (25 loc) · 809 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
import base64
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
backend = default_backend()
salt = b'\xd5z\xe49\xca\xd0\xa8\xd6\xb0\x0e\x1c\xc9\x80\xb2t2'
# Generate a key
# passw is the user password
# writeToFile is boolean
def gen_key(passw, writeToFile):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=backend
)
# Generate key
key = base64.urlsafe_b64encode(kdf.derive(passw.encode()))
# Write to file if writeToFile is True else return the key
if writeToFile:
with open("key.key", "wb") as key_file:
key_file.write(key)
else:
return key