refactor; rename alpha to prop

This commit is contained in:
2025-03-10 10:05:49 -05:00
parent 571268b86a
commit dd0b496a21
7 changed files with 33 additions and 38 deletions

113
src/user_cipher.py Normal file
View File

@@ -0,0 +1,113 @@
import base64
import hashlib
from dataclasses import dataclass
import bcrypt
from secrets import choice
from src.models import EncipheredNKode, KeypadSize
from src.customer_cipher import CustomerCipher
from src.utils import generate_random_nonrepeating_list, xor_lists, int_array_to_bytes
@dataclass
class UserCipher:
prop_key: list[int]
set_key: list[int]
pass_key: list[int]
mask_key: list[int]
salt: bytes
max_nkode_len: int
@classmethod
def create(cls, keypad_size: KeypadSize, set_values: list[int], max_nkode_len: int) -> 'UserCipher':
if len(set_values) != keypad_size.props_per_key:
raise ValueError("Invalid set values")
set_key = generate_random_nonrepeating_list(keypad_size.props_per_key)
set_key = xor_lists(set_key, set_values)
return UserCipher(
prop_key=generate_random_nonrepeating_list(keypad_size.props_per_key * keypad_size.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]:
if len(user_mask) >= self.max_nkode_len:
raise ValueError("User mask is too long")
padded_user_mask = user_mask.copy()
for _ in range(self.max_nkode_len - len(user_mask)):
padded_user_mask.append(choice(set_vals))
return padded_user_mask
@staticmethod
def encode_base64_str(data: list[int]) -> str:
return base64.b64encode(int_array_to_bytes(data)).decode("utf-8")
@staticmethod
def decode_base64_str(data: str) -> list[int]:
byte_data = base64.b64decode(data)
int_list = []
for i in range(0, len(byte_data), 2):
int_val = int.from_bytes(byte_data[i:i + 2], byteorder='big')
int_list.append(int_val)
return int_list
def _hash_passcode(self, passcode: list[int]) -> str:
passcode_bytes = int_array_to_bytes(passcode)
passcode_digest = base64.b64encode(hashlib.sha256(passcode_bytes).digest())
hashed_data = bcrypt.hashpw(passcode_digest, self.salt)
return hashed_data.decode("utf-8")
def encipher_nkode(
self,
passcode_prop_idx: list[int],
customer_cipher: CustomerCipher
) -> EncipheredNKode:
passcode_attrs = [customer_cipher.prop_key[idx] for idx in passcode_prop_idx]
passcode_sets = [customer_cipher.get_prop_set_val(attr) for attr in passcode_attrs]
mask = self.encipher_mask(passcode_sets, customer_cipher)
code = self.encipher_salt_hash_code(passcode_prop_idx, customer_cipher)
return EncipheredNKode(
code=code,
mask=mask
)
def encipher_salt_hash_code(
self,
passcode_prop_idx: list[int],
customer_prop: CustomerCipher,
) -> str:
passcode_len = len(passcode_prop_idx)
passcode_attrs = [customer_prop.prop_key[idx] for idx in passcode_prop_idx]
passcode_cipher = self.pass_key.copy()
for idx in range(passcode_len):
attr_idx = passcode_prop_idx[idx]
passcode_cipher[idx] ^= self.prop_key[attr_idx] ^ passcode_attrs[idx]
return self._hash_passcode(passcode_cipher)
def encipher_mask(
self,
passcode_sets: list[int],
customer_attributes: CustomerCipher
) -> str:
padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_attributes.set_key)
set_idx = [customer_attributes.get_set_index(set_val) for set_val in padded_passcode_sets]
mask_set_keys = [self.set_key[idx] for idx in set_idx]
ciphered_mask = xor_lists(mask_set_keys, padded_passcode_sets)
ciphered_mask = xor_lists(ciphered_mask, self.mask_key)
mask = self.encode_base64_str(ciphered_mask)
return mask
def decipher_mask(self, mask: str, set_vals: list, passcode_len: int) -> list[int]:
decoded_mask = self.decode_base64_str(mask)
deciphered_mask = xor_lists(decoded_mask, self.mask_key)
set_key_rand_component = xor_lists(set_vals, self.set_key)
passcode_sets = []
for set_cipher in deciphered_mask[:passcode_len]:
set_idx = set_key_rand_component.index(set_cipher)
passcode_sets.append(set_vals[set_idx])
return passcode_sets