refactor np.bitwise xor to ^ operator

This commit is contained in:
2025-03-16 06:20:05 -05:00
parent daebb61e56
commit b6ab0c1890
5 changed files with 149 additions and 158 deletions

View File

@@ -58,8 +58,8 @@ class Customer:
new_props = self.cipher.prop_key
new_sets = self.cipher.set_key
props_xor = np.bitwise_xor(new_props, old_props)
set_xor = np.bitwise_xor(new_sets, old_sets)
props_xor = new_props ^ old_props
set_xor = new_sets ^ old_sets
for user in self.users.values():
user.renew_keys(set_xor, props_xor)
self.users[user.username] = user

View File

@@ -18,8 +18,8 @@ class User:
def renew_keys(self, set_xor: np.ndarray, prop_xor: np.ndarray):
self.renew = True
self.cipher.set_key = np.bitwise_xor(self.cipher.set_key, set_xor)
self.cipher.prop_key = np.bitwise_xor(self.cipher.prop_key, prop_xor)
self.cipher.combined_set_key = self.cipher.combined_set_key ^ set_xor
self.cipher.prop_key = self.cipher.prop_key ^ prop_xor
def refresh_passcode(self, passcode_prop_idxs: list[int], customer_cipher: CustomerCipher):
self.cipher = UserCipher.create(

View File

@@ -11,7 +11,7 @@ from src.customer_cipher import CustomerCipher
@dataclass
class UserCipher:
prop_key: np.ndarray
set_key: np.ndarray
combined_set_key: np.ndarray
pass_key: np.ndarray
mask_key: np.ndarray
salt: bytes
@@ -30,7 +30,7 @@ class UserCipher:
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,
combined_set_key=set_key,
salt=bcrypt.gensalt(),
max_nkode_len=max_nkode_len
)
@@ -38,15 +38,11 @@ class UserCipher:
def pad_user_mask(self, user_mask: np.ndarray, set_vals: np.ndarray) -> np.ndarray:
if len(user_mask) >= self.max_nkode_len:
raise ValueError("User mask is too long")
user_mask_array = np.array(user_mask, dtype=np.uint16)
# Create padding of random choices from set_vals
padding_size = self.max_nkode_len - len(user_mask)
padding_indices = np.random.choice(len(set_vals), padding_size)
padding = np.array([set_vals[i] for i in padding_indices], dtype=np.uint16)
# Generate padding directly using np.random.choice
padding = np.random.choice(set_vals, size=padding_size, replace=True).astype(np.uint16)
# Concatenate original mask with padding
padded_user_mask = np.concatenate([user_mask_array, padding])
return padded_user_mask
return np.concatenate([user_mask, padding])
@staticmethod
def encode_base64_str(data: np.ndarray) -> str:
@@ -108,7 +104,7 @@ class UserCipher:
# Get indices of set values
set_idx = np.array([customer_cipher.get_set_index(set_val) for set_val in padded_customer_sets],
dtype=np.uint16)
mask_set_keys = np.array([self.set_key[idx] for idx in set_idx], dtype=np.uint16)
mask_set_keys = np.array([self.combined_set_key[idx] for idx in set_idx], dtype=np.uint16)
# XOR operations
ciphered_mask = np.bitwise_xor(mask_set_keys, padded_customer_sets)
@@ -122,7 +118,7 @@ class UserCipher:
decoded_mask = self.decode_base64_str(mask)
deciphered_mask = np.bitwise_xor(decoded_mask, self.mask_key)
set_key_rand_component = np.bitwise_xor(set_vals_array, self.set_key)
set_key_rand_component = np.bitwise_xor(set_vals_array, self.combined_set_key)
passcode_sets = []
for set_cipher in deciphered_mask[:passcode_len]: