implement encipher

This commit is contained in:
2024-07-11 08:53:25 -05:00
parent 11ae1ea759
commit 46588e13ac
4 changed files with 89 additions and 16 deletions

View File

@@ -1,14 +0,0 @@
from src.models import (
CustomerInterface,
UserEncipherKeys,
EncipheredNKode
)
def encipher_nkode(
user_nkode_attributes: list[int],
user_nkode_sets: list[int],
user_keys: UserEncipherKeys,
customer_interface: CustomerInterface
) -> EncipheredNKode:
pass

57
src/encipher_nkode.py Normal file
View File

@@ -0,0 +1,57 @@
from secrets import choice
import base64
from src.models import (
CustomerInterface,
UserEncipherKeys,
EncipheredNKode,
CustomerAttribute
)
def pad_user_mask(user_mask: list[int], customer_interface: CustomerInterface, max_nkode_len: int) -> list[int]:
assert (len(user_mask) <= max_nkode_len)
set_vals = customer_interface.get_set_values()
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
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))]
def encode_base64_str(data: list[int]) -> str:
return base64.b64encode(data).decode("utf-8")
def decode_base64_str(data: str) -> list[int]:
return list(base64.b64decode(data))
def hash_data(data: list[int]) -> bytes:
pass
def encipher_nkode(
user_nkode_attributes: list[CustomerAttribute],
user_keys: UserEncipherKeys,
customer_interface: CustomerInterface
) -> 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 = pad_user_mask(user_nkode_mask, customer_interface, max_nkode_len)
passcode_cipher = 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 ^= user_keys.alpha_key[set_idx][attr_idx] ^ user_keys.pass_key[idx]
mask_cipher ^= user_keys.set_key[set_idx] ^ user_keys.mask_key[idx]
return EncipheredNKode(
code="",
mask=""
)

View File

@@ -10,6 +10,10 @@ class UserEncipherKeys(BaseModel):
set_key: list[int]
pass_key: list[int]
mask_key: list[int]
# TODO: add these
# height
# width
# max_nkode_len
@staticmethod
def new_user_encipher_keys(height: int, width: int, set_values: list[int]):
@@ -32,13 +36,23 @@ class CustomerAttribute(BaseModel):
class CustomerInterface(BaseModel):
# base_interface: { set0: [attr0 ... attrN], ... setM: [(val0, setM) ... (valN, setM)]}
# base_interface: { set0: [(attr0, set0) ... (attrN, set0)], ... setM: [(attr0, setM) ... (attrN, setM)]}
base_interface: dict[int, CustomerAttribute]
height: int
width: int
def __init__(self, **data):
super().__init__(**data)
self._set_index_lookup: dict[int, int] = {}
self._attr_index_lookup: dict[CustomerAttribute: (int, int)] = {}
self._map_base_interface()
def _map_base_interface(self):
for set_idx, set_val in enumerate(self.base_interface.keys()):
attrs = self.base_interface[set_val]
self._set_index_lookup[set_val] = set_idx
for attr_idx, attr in enumerate(attrs):
self._attr_index_lookup[attr] = attr_idx
@staticmethod
def new_interface(height: int, width: int):
@@ -58,12 +72,22 @@ class CustomerInterface(BaseModel):
possible_set_vals.remove(cur_set)
return CustomerInterface(
base_interface=base_interface
base_interface=base_interface,
height=height,
width=width,
)
def get_set_values(self) -> list[int]:
return list(self.base_interface.keys())
def get_attr_index(self, attr: CustomerAttribute) -> int:
assert(attr in self._attr_index_lookup.keys())
return self._attr_index_lookup[attr]
def get_set_index(self, set_val: int) -> int:
assert(set_val in self.get_set_values())
return self._set_index_lookup[set_val]
class EncipheredNKode(BaseModel):
code: str

6
test/test_encipher.py Normal file
View File

@@ -0,0 +1,6 @@
import pytest
from src.encipher_nkode import encipher_nkode
def test_encipher_nkode():
pass