-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
52 lines (43 loc) · 1.39 KB
/
utils.py
File metadata and controls
52 lines (43 loc) · 1.39 KB
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, os, secrets, string
KEY_FILE = "key.key"
# Générer ou charger clé
def load_key():
if not os.path.exists(KEY_FILE):
key = Fernet.generate_key()
with open(KEY_FILE, "wb") as f:
f.write(key)
else:
with open(KEY_FILE, "rb") as f:
key = f.read()
return Fernet(key)
fernet = load_key()
# Chiffre un dictionnaire
def encrypt_data(data: dict):
return fernet.encrypt(json.dumps(data).encode())
# Déchiffre
def decrypt_data(enc_data: bytes):
return json.loads(fernet.decrypt(enc_data).decode())
# Charger base
def load_db(path="db.json.enc"):
if not os.path.exists(path):
return {}
with open(path, "rb") as f:
return decrypt_data(f.read())
# Sauvegarder base
def save_db(data: dict, path="db.json.enc"):
with open(path, "wb") as f:
f.write(encrypt_data(data))
# Générer mot de passe fort
def generate_password(length=16):
alphabet = string.ascii_letters + string.digits + string.punctuation
return ''.join(secrets.choice(alphabet) for _ in range(length))
# Vérifier force mot de passe
def is_strong(password):
return (
len(password) >= 12 and
any(c.islower() for c in password) and
any(c.isupper() for c in password) and
any(c.isdigit() for c in password) and
any(c in string.punctuation for c in password)
)