-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
30 lines (23 loc) · 785 Bytes
/
encryption.py
File metadata and controls
30 lines (23 loc) · 785 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
30
from cryptography.fernet import Fernet
import bcrypt
import os
KEY_FILE = "key.key"
def load_or_create_key():
if os.path.exists(KEY_FILE):
with open(KEY_FILE, 'rb') as f:
return f.read()
else:
key = Fernet.generate_key()
with open(KEY_FILE, 'wb') as f:
f.write(key)
return key
key = load_or_create_key()
cipher_suite = Fernet(key)
def encrypt_password(password):
return cipher_suite.encrypt(password.encode())
def decrypt_password(encrypted_password):
return cipher_suite.decrypt(encrypted_password).decode()
def hash_master_password(password):
return bcrypt.hashpw(password.encode(), bcrypt.gensalt())
def verify_master_password(password, hashed):
return bcrypt.checkpw(password.encode(), hashed)