remove unneeded functions
This commit is contained in:
@@ -19,7 +19,7 @@
|
|||||||
"import numpy as np\n",
|
"import numpy as np\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def keypad_md_table(keypad_list: np.ndarray, keypad_size: KeypadSize) -> str:\n",
|
"def keypad_md_table(keypad_list: np.ndarray, keypad_size: KeypadSize) -> str:\n",
|
||||||
" assert (keypad_size.numb_of_props == len(keypad_list))\n",
|
" assert (keypad_size.total_props == len(keypad_list))\n",
|
||||||
" keypad = keypad_list.reshape(-1, keypad_size.props_per_key)\n",
|
" keypad = keypad_list.reshape(-1, keypad_size.props_per_key)\n",
|
||||||
" table = \"|key|\" + \"\".join([f\"set{idx}|\" for idx in range(keypad_size.props_per_key)])\n",
|
" table = \"|key|\" + \"\".join([f\"set{idx}|\" for idx in range(keypad_size.props_per_key)])\n",
|
||||||
" table += \"\\n|\" + \"\".join(\"-|\" for _ in range(keypad_size.props_per_key + 1))\n",
|
" table += \"\\n|\" + \"\".join(\"-|\" for _ in range(keypad_size.props_per_key + 1))\n",
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class CustomerCipher:
|
|||||||
raise ValueError(f"Keys and properties per key must not exceed {cls.MAX_KEYS}")
|
raise ValueError(f"Keys and properties per key must not exceed {cls.MAX_KEYS}")
|
||||||
|
|
||||||
# Using numpy to generate non-repeating random integers
|
# Using numpy to generate non-repeating random integers
|
||||||
prop_key = np.random.choice(2 ** 16, size=keypad_size.numb_of_props, replace=False)
|
prop_key = np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False)
|
||||||
set_key = np.random.choice(2 ** 16, size=keypad_size.props_per_key, replace=False)
|
set_key = np.random.choice(2 ** 16, size=keypad_size.props_per_key, replace=False)
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
@@ -36,7 +36,7 @@ class CustomerCipher:
|
|||||||
)
|
)
|
||||||
|
|
||||||
def renew(self):
|
def renew(self):
|
||||||
self.prop_key = np.random.choice(2 ** 16, size=self.keypad_size.numb_of_props, replace=False)
|
self.prop_key = np.random.choice(2 ** 16, size=self.keypad_size.total_props, replace=False)
|
||||||
self.set_key = np.random.choice(2 ** 16, size=self.keypad_size.props_per_key, replace=False)
|
self.set_key = np.random.choice(2 ** 16, size=self.keypad_size.props_per_key, replace=False)
|
||||||
|
|
||||||
def get_prop_set_val(self, prop: int) -> int:
|
def get_prop_set_val(self, prop: int) -> int:
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class KeypadSize:
|
|||||||
numb_of_keys: int
|
numb_of_keys: int
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def numb_of_props(self) -> int:
|
def total_props(self) -> int:
|
||||||
return self.props_per_key * self.numb_of_keys
|
return self.props_per_key * self.numb_of_keys
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -23,13 +23,13 @@ class UserCipher:
|
|||||||
raise ValueError("Invalid set values")
|
raise ValueError("Invalid set values")
|
||||||
|
|
||||||
set_values_array = np.array(set_values, dtype=np.uint16)
|
set_values_array = np.array(set_values, dtype=np.uint16)
|
||||||
set_key = generate_random_nonrepeating_array(keypad_size.props_per_key)
|
set_key = np.random.choice(2**16,size=keypad_size.props_per_key, replace=False)
|
||||||
set_key = np.bitwise_xor(set_key, set_values_array)
|
set_key = np.bitwise_xor(set_key, set_values_array)
|
||||||
|
|
||||||
return UserCipher(
|
return UserCipher(
|
||||||
prop_key=generate_random_nonrepeating_array(keypad_size.props_per_key * keypad_size.numb_of_keys),
|
prop_key=np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False),
|
||||||
pass_key=generate_random_nonrepeating_array(max_nkode_len),
|
pass_key=np.random.choice(2 ** 16, size=max_nkode_len, replace=False),
|
||||||
mask_key=generate_random_nonrepeating_array(max_nkode_len),
|
mask_key=np.random.choice(2**16, size=max_nkode_len, replace=False),
|
||||||
set_key=set_key,
|
set_key=set_key,
|
||||||
salt=bcrypt.gensalt(),
|
salt=bcrypt.gensalt(),
|
||||||
max_nkode_len=max_nkode_len
|
max_nkode_len=max_nkode_len
|
||||||
@@ -63,7 +63,7 @@ class UserCipher:
|
|||||||
return np.array(int_list, dtype=np.uint16)
|
return np.array(int_list, dtype=np.uint16)
|
||||||
|
|
||||||
def _hash_passcode(self, passcode: np.ndarray) -> str:
|
def _hash_passcode(self, passcode: np.ndarray) -> str:
|
||||||
passcode_bytes = int_array_to_bytes(passcode)
|
passcode_bytes = passcode.astype(np.uint16).tobytes()
|
||||||
passcode_digest = base64.b64encode(hashlib.sha256(passcode_bytes).digest())
|
passcode_digest = base64.b64encode(hashlib.sha256(passcode_bytes).digest())
|
||||||
hashed_data = bcrypt.hashpw(passcode_digest, self.salt)
|
hashed_data = bcrypt.hashpw(passcode_digest, self.salt)
|
||||||
return hashed_data.decode("utf-8")
|
return hashed_data.decode("utf-8")
|
||||||
@@ -88,20 +88,20 @@ class UserCipher:
|
|||||||
passcode_prop_idx: list[int],
|
passcode_prop_idx: list[int],
|
||||||
customer_prop: CustomerCipher,
|
customer_prop: CustomerCipher,
|
||||||
) -> str:
|
) -> str:
|
||||||
passcode_prop_idx_array = np.array(passcode_prop_idx, dtype=np.uint16)
|
passcode_len = len(passcode_prop_idx)
|
||||||
passcode_len = len(passcode_prop_idx_array)
|
|
||||||
passcode_props = np.array([customer_prop.prop_key[idx] for idx in passcode_prop_idx_array], dtype=np.uint16)
|
|
||||||
|
|
||||||
passcode_cipher = self.pass_key.copy()
|
passcode_cipher = self.pass_key.copy()
|
||||||
for idx in range(passcode_len):
|
passcode_cipher[:passcode_len] = (
|
||||||
prop_idx = passcode_prop_idx_array[idx]
|
passcode_cipher[:passcode_len] ^
|
||||||
passcode_cipher[idx] = passcode_cipher[idx] ^ self.prop_key[prop_idx] ^ passcode_props[idx]
|
self.prop_key[passcode_prop_idx] ^
|
||||||
|
customer_prop.prop_key[passcode_prop_idx]
|
||||||
|
)
|
||||||
|
|
||||||
return self._hash_passcode(passcode_cipher)
|
return self._hash_passcode(passcode_cipher)
|
||||||
|
|
||||||
|
|
||||||
def encipher_mask(
|
def encipher_mask(
|
||||||
self,
|
self,
|
||||||
passcode_sets: list[int],
|
passcode_sets: np.ndarray,
|
||||||
customer_properites: CustomerCipher
|
customer_properites: CustomerCipher
|
||||||
) -> str:
|
) -> str:
|
||||||
padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_properites.set_key)
|
padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_properites.set_key)
|
||||||
@@ -131,21 +131,7 @@ class UserCipher:
|
|||||||
set_idx = np.where(set_key_rand_component == set_cipher)[0][0]
|
set_idx = np.where(set_key_rand_component == set_cipher)[0][0]
|
||||||
passcode_sets.append(set_vals[set_idx])
|
passcode_sets.append(set_vals[set_idx])
|
||||||
|
|
||||||
return passcode_sets
|
return np.array(passcode_sets)
|
||||||
|
|
||||||
|
|
||||||
# NumPy utility functions to replace the existing ones
|
|
||||||
def generate_random_nonrepeating_array(array_len: int, min_val: int = 0, max_val: int = 2 ** 16) -> np.ndarray:
|
|
||||||
if max_val - min_val < array_len:
|
|
||||||
raise ValueError("Range of values is less than the array length requested")
|
|
||||||
|
|
||||||
# Generate array of random unique integers
|
|
||||||
return np.random.choice(
|
|
||||||
np.arange(min_val, max_val, dtype=np.uint16),
|
|
||||||
size=array_len,
|
|
||||||
replace=False
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def int_array_to_bytes(int_arr: np.ndarray, byte_size: int = 2) -> bytes:
|
def int_array_to_bytes(int_arr: np.ndarray, byte_size: int = 2) -> bytes:
|
||||||
return b"".join([int(num).to_bytes(byte_size, byteorder='big') for num in int_arr])
|
return b"".join([int(num).to_bytes(byte_size, byteorder='big') for num in int_arr])
|
||||||
@@ -11,7 +11,7 @@ class UserKeypad:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, keypad_size: KeypadSize) -> 'UserKeypad':
|
def create(cls, keypad_size: KeypadSize) -> 'UserKeypad':
|
||||||
keypad = UserKeypad(
|
keypad = UserKeypad(
|
||||||
keypad=np.arange(keypad_size.numb_of_props),
|
keypad=np.arange(keypad_size.total_props),
|
||||||
keypad_size=keypad_size
|
keypad_size=keypad_size
|
||||||
)
|
)
|
||||||
keypad.random_keypad_shuffle()
|
keypad.random_keypad_shuffle()
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from src.models import KeypadSize
|
|||||||
)
|
)
|
||||||
def test_prop_set_idx(keypad_size):
|
def test_prop_set_idx(keypad_size):
|
||||||
user_keypad = UserKeypad.create(keypad_size)
|
user_keypad = UserKeypad.create(keypad_size)
|
||||||
for prop_idx in range(keypad_size.numb_of_props):
|
for prop_idx in range(keypad_size.total_props):
|
||||||
user_keypad_idx = user_keypad.keypad[prop_idx]
|
user_keypad_idx = user_keypad.keypad[prop_idx]
|
||||||
|
|
||||||
assert (prop_idx % keypad_size.props_per_key == user_keypad_idx % keypad_size.props_per_key)
|
assert (prop_idx % keypad_size.props_per_key == user_keypad_idx % keypad_size.props_per_key)
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ def test_encode_decode_base64(passcode_len):
|
|||||||
def test_decode_mask(keypad_size, max_nkode_len):
|
def test_decode_mask(keypad_size, max_nkode_len):
|
||||||
customer = CustomerCipher.create(keypad_size)
|
customer = CustomerCipher.create(keypad_size)
|
||||||
#passcode_entry = generate_random_nonrepeating_list(keypad_size.numb_of_props,max_val=keypad_size.numb_of_props)[:4]
|
#passcode_entry = generate_random_nonrepeating_list(keypad_size.numb_of_props,max_val=keypad_size.numb_of_props)[:4]
|
||||||
passcode_entry = np.random.choice(keypad_size.numb_of_props, 4, replace=False)
|
passcode_entry = np.random.choice(keypad_size.total_props, 4, replace=False)
|
||||||
passcode_values = [customer.prop_key[idx] for idx in passcode_entry]
|
passcode_values = [customer.prop_key[idx] for idx in passcode_entry]
|
||||||
set_vals = customer.set_key
|
set_vals = customer.set_key
|
||||||
user_keys = UserCipher.create(keypad_size, set_vals, max_nkode_len)
|
user_keys = UserCipher.create(keypad_size, set_vals, max_nkode_len)
|
||||||
|
|||||||
Reference in New Issue
Block a user