database access for rooms and mqtt client + generator

This commit is contained in:
2025-12-09 09:07:07 +01:00
parent 75f4b7e1eb
commit 0a7b029141
16 changed files with 486 additions and 96 deletions

41
user_handling.py Normal file
View File

@@ -0,0 +1,41 @@
from os import urandom
from typing import Tuple, Optional
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
import base64
with open("key.txt", "rb") as key_file:
key = key_file.read().strip()
f = Fernet(key)
#TODO: properly set SQL blob sizes, they're constant
def __scrypt__(salt: bytes) -> Scrypt:
return Scrypt(
salt=salt,
length=32,
n=2**14,
r=8,
p=1,
)
def new_password(password: bytes) -> Tuple[bytes, bytes]:
salt = urandom(16)
key = base64.urlsafe_b64encode(__scrypt__(salt).derive(password))
f = Fernet(key)
out = f.encrypt(password)
return salt, out
def verify_password(stored_salt: bytes, stored_key: bytes, provided_password: bytes) -> bytes:
key = base64.urlsafe_b64encode(__scrypt__(stored_salt).derive(provided_password))
f = Fernet(key)
return f.decrypt(stored_key)
if __name__ == "__main__":
# helper script for inserting new users
import db_connect
username = input("Enter new username: ")
password = input("Enter new password: ").encode()
db = db_connect.DatabaseConnect()
salt, key = new_password(password)
db.create_user(username, salt, key)
print(f"Created user {username}")