split up models

This commit is contained in:
2024-07-12 19:28:47 -05:00
parent ba14ae174c
commit c71d29c076
5 changed files with 114 additions and 142 deletions

82
src/user_cipher_keys.py Normal file
View File

@@ -0,0 +1,82 @@
import base64
import hashlib
import bcrypt
from secrets import choice
from pydantic import BaseModel
from src.models import NKodeAttribute, EncipheredNKode
from src.nkode_interface import NKodeInterface
from src.utils import generate_random_nonrepeating_list, xor_lists, generate_random_nonrepeating_matrix
class UserCipherKeys(BaseModel):
alpha_key: list[list[int]]
set_key: list[int]
pass_key: list[int]
mask_key: list[int]
salt: bytes
@staticmethod
def new_user_encipher_keys(numb_of_keys: int, attrs_per_key: int, set_values: list[int]):
assert len(set_values) == attrs_per_key
set_key = generate_random_nonrepeating_list(attrs_per_key)
set_key = xor_lists(set_key, set_values)
return UserCipherKeys(
alpha_key=generate_random_nonrepeating_matrix(attrs_per_key, numb_of_keys),
pass_key=generate_random_nonrepeating_list(numb_of_keys),
mask_key=generate_random_nonrepeating_list(numb_of_keys),
set_key=set_key,
salt=bcrypt.gensalt(),
)
@staticmethod
def pad_user_mask(user_mask: list[int], customer_interface: NKodeInterface, max_nkode_len: int) -> list[int]:
assert (len(user_mask) <= max_nkode_len)
set_vals = customer_interface.set_
padded_user_mask = user_mask.copy()
for _ in range(max_nkode_len - len(user_mask)):
padded_user_mask.append(choice(set_vals))
return padded_user_mask
@staticmethod
def pad_user_code(user_code: list[int], max_nkode_len: int) -> list[int]:
assert (len(user_code) <= max_nkode_len)
return user_code + [0 for _ in range(max_nkode_len - len(user_code))]
@staticmethod
def encode_base64_str(data: list[int]) -> str:
return base64.b64encode(data).decode("utf-8")
@staticmethod
def decode_base64_str(data: str) -> list[int]:
return list(base64.b64decode(data))
def _hash_passcode(self, passcode: list[int]) -> str:
passcode_digest = base64.b64encode(hashlib.sha256(passcode).digest())
hashed_data = bcrypt.hashpw(passcode_digest, self.salt)
return hashed_data.decode("utf-8")
def encipher_nkode(
self,
nkode_attr_index: list[NKodeAttribute],
customer_interface: NKodeInterface
) -> EncipheredNKode:
max_nkode_len = 10
user_nkode_mask = [attr.set_val for attr in user_nkode_attributes]
user_nkode_attrs = [attr.attr_val for attr in user_nkode_attributes]
mask_cipher = self.pad_user_mask(user_nkode_mask, customer_interface, max_nkode_len)
passcode_cipher = self.pad_user_code(user_nkode_attrs, max_nkode_len)
for idx in range(max_nkode_len):
set_idx = customer_interface.get_set_index(user_nkode_attributes[idx].set_val)
attr_idx = customer_interface.get_set_index(user_nkode_attributes[idx].attr_val)
passcode_cipher ^= self.alpha_key[set_idx][attr_idx] ^ self.pass_key[idx]
mask_cipher ^= self.set_key[set_idx] ^ self.mask_key[idx]
return EncipheredNKode(
code=self._hash_passcode(passcode_cipher),
mask=self.base64.b64encode(mask_cipher).decode("utf-8")
)