Image info, endpoint encryption

This commit is contained in:
BarsTiger
2023-02-24 23:41:29 +02:00
parent 846e65e2ce
commit 8f0cd0ac05
19 changed files with 211 additions and 23 deletions

View File

@@ -1,2 +1,3 @@
from .db import db
from .db_model import DBTables
from .encryption import encrypt, decrypt

View File

@@ -11,5 +11,6 @@ db = {
'cooldown': DBDict(DB, autocommit=True, tablename='cooldown'),
'exceptions': DBDict(DB, autocommit=True, tablename='exceptions'),
'queue': DBDict(DB, autocommit=True, tablename='queue'),
'generated': DBDict(DB, autocommit=True, tablename='generated')
'generated': DBDict(DB, autocommit=True, tablename='generated'),
'prompts': DBDict(DB, autocommit=True, tablename='prompts')
}

View File

@@ -8,12 +8,13 @@ from .meta import DBMeta
class DBTables:
tables = ['config', 'cooldown', 'exceptions', 'queue', 'generated']
tables = ['config', 'cooldown', 'exceptions', 'queue', 'generated', 'prompts']
config = "config"
cooldown = "cooldown"
exceptions = "exceptions"
queue = "queue"
generated = "generated"
prompts = "prompts"
class DBDict(SqliteDict):

25
bot/db/encryption.py Normal file
View File

@@ -0,0 +1,25 @@
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64
from bot.config import ENCRYPTION_KEY, BARS_APP_ID
fernet = Fernet(
base64.urlsafe_b64encode(
PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
iterations=390000,
salt=BARS_APP_ID.encode()
).derive(ENCRYPTION_KEY)
)
)
def encrypt(s: str) -> bytes:
return fernet.encrypt(s.encode())
def decrypt(s: bytes) -> str:
return fernet.decrypt(s).decode()