rename functions and attributes

This commit is contained in:
2024-07-17 08:59:59 -05:00
parent c4d8233730
commit c907f159ab
9 changed files with 42 additions and 40 deletions

View File

@@ -2,7 +2,7 @@ from uuid import UUID
from pydantic import BaseModel
from src.customer_interface import CustomerInterface
from src.models import NKodePolicy
from src.user_db_model import UserDBModel
from src.user import User
from src.utils import xor_lists
@@ -10,9 +10,9 @@ class Customer(BaseModel):
customer_id: UUID
nkode_policy: NKodePolicy
interface: CustomerInterface
users: dict[str, UserDBModel]
users: dict[str, User]
def add_new_user(self, user: UserDBModel):
def add_new_user(self, user: User):
self.users[user.username] = user
def valid_key_entry(self, username, selected_keys) -> bool:

View File

@@ -3,7 +3,7 @@ from uuid import UUID
from pydantic import BaseModel
class SessionCacheModel(BaseModel):
class SessionCache(BaseModel):
session_id: UUID
set_interface: list[int] | None = None
confirm_interface: list[int] | None = None

View File

@@ -7,7 +7,7 @@ from src.user_interface import UserInterface
from src.utils import xor_lists
class UserDBModel(BaseModel):
class User(BaseModel):
username: str
enciphered_passcode: EncipheredNKode
user_keys: UserCipherKeys
@@ -23,8 +23,8 @@ class UserDBModel(BaseModel):
self.user_keys = UserCipherKeys.new(
customer_interface.numb_of_keys,
customer_interface.attrs_per_key,
customer_interface.set_vals
customer_interface.set_vals,
self.user_keys.max_nkode_len
)
self.enciphered_passcode = self.user_keys.encipher_nkode(passcode_attr_idx, customer_interface)
self.renew = False

View File

@@ -15,10 +15,10 @@ class UserCipherKeys(BaseModel):
pass_key: list[int]
mask_key: list[int]
salt: bytes
max_nkode_len: int = 10
max_nkode_len: int
@staticmethod
def new(numb_of_keys: int, attrs_per_key: int, set_values: list[int]):
def new(numb_of_keys: int, attrs_per_key: int, set_values: list[int], max_nkode_len: int):
assert len(set_values) == attrs_per_key
set_key = generate_random_nonrepeating_list(attrs_per_key)
@@ -26,10 +26,11 @@ class UserCipherKeys(BaseModel):
return UserCipherKeys(
alpha_key=generate_random_nonrepeating_list(attrs_per_key * numb_of_keys),
pass_key=generate_random_nonrepeating_list(numb_of_keys),
mask_key=generate_random_nonrepeating_list(numb_of_keys),
pass_key=generate_random_nonrepeating_list(max_nkode_len),
mask_key=generate_random_nonrepeating_list(max_nkode_len),
set_key=set_key,
salt=bcrypt.gensalt(),
max_nkode_len=max_nkode_len
)
def pad_user_mask(self, user_mask: list[int], set_vals: list[int]) -> list[int]:

View File

@@ -4,7 +4,7 @@ from src.utils import list_to_matrix, secure_fisher_yates_shuffle, matrix_to_lis
class UserInterface(BaseModel):
interface_index: list[int]
attr_indices: list[int]
attrs_per_key: int
numb_of_keys: int
@@ -12,7 +12,7 @@ class UserInterface(BaseModel):
def new(attrs_per_key: int, numb_of_keys: int):
# Todo: this a hack do a proper random interface
interface = UserInterface(
interface_index=list(range(attrs_per_key * numb_of_keys)),
attr_indices=list(range(attrs_per_key * numb_of_keys)),
attrs_per_key=attrs_per_key,
numb_of_keys=numb_of_keys,
)
@@ -22,10 +22,10 @@ class UserInterface(BaseModel):
return interface
def disperse_interface(self):
user_interface_matrix = list_to_matrix(self.interface_index, self.attrs_per_key)
user_interface_matrix = list_to_matrix(self.attr_indices, self.attrs_per_key)
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
dispersed_interface = self._random_attribute_rotation(shuffled_keys, list(range(self.attrs_per_key)))
self.interface_index = matrix_to_list(dispersed_interface)
self.attr_indices = matrix_to_list(dispersed_interface)
@staticmethod
def matrix_transpose(interface: list[list[int]]) -> list[list[int]]:
@@ -37,7 +37,7 @@ class UserInterface(BaseModel):
# randomly shuffle half the sets. if attrs_per_key is odd, randomly add one 50% of the time
numb_of_selected_sets += choice([0, 1]) if (self.attrs_per_key & 1) == 1 else 0
selected_sets = secure_fisher_yates_shuffle(list(range(self.attrs_per_key)))[:numb_of_selected_sets]
user_interface_matrix = list_to_matrix(self.interface_index, self.attrs_per_key)
user_interface_matrix = list_to_matrix(self.attr_indices, self.attrs_per_key)
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
interface_by_sets = []
for idx, attrs in enumerate(self.matrix_transpose(shuffled_keys)):
@@ -45,7 +45,7 @@ class UserInterface(BaseModel):
interface_by_sets.append(secure_fisher_yates_shuffle(attrs))
else:
interface_by_sets.append(attrs)
self.interface_index = matrix_to_list(self.matrix_transpose(interface_by_sets))
self.attr_indices = matrix_to_list(self.matrix_transpose(interface_by_sets))
def _random_attribute_rotation(self, user_interface: list[list[int]], selected_sets: list[int]) -> list[list[int]]:
attr_rotation = secure_fisher_yates_shuffle(list(range(self.numb_of_keys)))[:self.attrs_per_key]
@@ -59,7 +59,7 @@ class UserInterface(BaseModel):
return self.matrix_transpose(transposed_user_interface)
def attribute_adjacency_graph(self) -> dict[int, set[int]]:
user_interface_keypad = list_to_matrix(self.interface_index, self.attrs_per_key)
user_interface_keypad = list_to_matrix(self.attr_indices, self.attrs_per_key)
graph = {}
for key in user_interface_keypad:
for attr in key:
@@ -70,5 +70,5 @@ class UserInterface(BaseModel):
def get_key_attr_idxs(self, key_numb: int) -> list[int]:
assert (0 <= key_numb < self.numb_of_keys)
keypad_attr_idx = list_to_matrix(self.interface_index, self.attrs_per_key)
keypad_attr_idx = list_to_matrix(self.attr_indices, self.attrs_per_key)
return keypad_attr_idx[key_numb]