remove unneeded functions

This commit is contained in:
2025-03-14 11:15:56 -05:00
parent 439b706fbd
commit 303f4a7457
7 changed files with 21 additions and 35 deletions

View File

@@ -23,13 +23,13 @@ class UserCipher:
raise ValueError("Invalid set values")
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)
return UserCipher(
prop_key=generate_random_nonrepeating_array(keypad_size.props_per_key * keypad_size.numb_of_keys),
pass_key=generate_random_nonrepeating_array(max_nkode_len),
mask_key=generate_random_nonrepeating_array(max_nkode_len),
prop_key=np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False),
pass_key=np.random.choice(2 ** 16, size=max_nkode_len, replace=False),
mask_key=np.random.choice(2**16, size=max_nkode_len, replace=False),
set_key=set_key,
salt=bcrypt.gensalt(),
max_nkode_len=max_nkode_len
@@ -63,7 +63,7 @@ class UserCipher:
return np.array(int_list, dtype=np.uint16)
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())
hashed_data = bcrypt.hashpw(passcode_digest, self.salt)
return hashed_data.decode("utf-8")
@@ -88,20 +88,20 @@ class UserCipher:
passcode_prop_idx: list[int],
customer_prop: CustomerCipher,
) -> str:
passcode_prop_idx_array = np.array(passcode_prop_idx, dtype=np.uint16)
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_len = len(passcode_prop_idx)
passcode_cipher = self.pass_key.copy()
for idx in range(passcode_len):
prop_idx = passcode_prop_idx_array[idx]
passcode_cipher[idx] = passcode_cipher[idx] ^ self.prop_key[prop_idx] ^ passcode_props[idx]
passcode_cipher[:passcode_len] = (
passcode_cipher[:passcode_len] ^
self.prop_key[passcode_prop_idx] ^
customer_prop.prop_key[passcode_prop_idx]
)
return self._hash_passcode(passcode_cipher)
def encipher_mask(
self,
passcode_sets: list[int],
passcode_sets: np.ndarray,
customer_properites: CustomerCipher
) -> str:
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]
passcode_sets.append(set_vals[set_idx])
return 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
)
return np.array(passcode_sets)
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])