This commit is contained in:
relikd
2023-10-02 23:39:20 +02:00
commit 8629b01da3
47 changed files with 1412 additions and 0 deletions

28
backend/app/utils.py Normal file
View File

@@ -0,0 +1,28 @@
from django.utils.crypto import get_random_string
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.hashes import Hash, SHA256
import os
import typing
if typing.TYPE_CHECKING:
from app.models.member import Member
def random_secret():
return get_random_string(16)
def overwrite_upload(instance: 'Member', filename: str):
if os.path.isfile(instance.image_os_path):
os.remove(instance.image_os_path)
return instance.image_save_url
def encrypt(plaintext: str, key: str):
digest = Hash(SHA256())
digest.update(key.encode('utf8'))
aesgcm = AESGCM(digest.finalize())
iv = os.urandom(12)
ct = aesgcm.encrypt(iv, plaintext.encode('utf8'), iv)
return iv + ct