-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_helper.py
52 lines (39 loc) · 1.27 KB
/
crypto_helper.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from cryptography.fernet import Fernet
import json
import base64
import os
import getpass
def generate_key():
key = Fernet.generate_key()
return key
def save_key_to_file(key, file_path):
with open(file_path, "wb") as key_file:
key_file.write(key)
def load_key(file_path):
with open(file_path, "rb") as key_file:
return key_file.read()
def prompt_credentials():
email = input("Enter your email: ")
password = getpass.getpass("Enter your password: ")
return {"identifier": email, "password": password}
def encode_credentials(credentials):
json_str = json.dumps(credentials)
return base64.b64encode(json_str.encode('utf-8'))
def encrypt_data(data, key):
f = Fernet(key)
return f.encrypt(data)
def main():
key_directory = ".secrets"
key_file = "secret.key"
key_path = os.path.join(key_directory, key_file)
if not os.path.exists(key_directory):
os.makedirs(key_directory)
key = generate_key()
save_key_to_file(key, key_path)
loaded_key = load_key(key_path)
credentials = prompt_credentials()
encoded_credentials = encode_credentials(credentials)
encrypted_credentials = encrypt_data(encoded_credentials, loaded_key)
print(encrypted_credentials)
if __name__ == "__main__":
main()