numpy refactor

This commit is contained in:
2025-03-13 04:40:45 -05:00
parent facd9ee318
commit f6bf731186
12 changed files with 261 additions and 140 deletions

View File

@@ -1,15 +1,14 @@
import numpy as np
from jinja2 import Environment, FileSystemLoader
import os
from src.nkode_api import NKodeAPI
from src.models import NKodePolicy, KeypadSize, EncipheredNKode
from src.user_cipher import UserCipher
from src.utils import list_to_matrix, matrix_transpose, xor_lists
from secrets import choice
from string import ascii_lowercase
import bcrypt
import hashlib
import base64
from src.utils import int_array_to_bytes
def random_username() -> str:
@@ -20,10 +19,10 @@ def select_keys_with_passcode_values(user_passcode: list[int], interface: list[i
return [interface.index(attr) // attrs_per_key for attr in user_passcode]
def keypad_view(interface: list[int], attrs_per_key: int):
def visualize_keypad(keypad_list: np.ndarray, props_per_key: int):
print("Keypad View")
interface_keypad = list_to_matrix(interface, attrs_per_key)
for idx, key_vals in enumerate(interface_keypad):
keypad_mat = keypad_list.reshape(-1, props_per_key)
for idx, key_vals in enumerate(keypad_mat):
print(f"Key {idx}: {key_vals}")
@@ -65,14 +64,15 @@ if __name__ == "__main__":
set_vals = customer.cipher.set_key
attr_vals = customer.cipher.prop_key
customer_attr_view = list_to_matrix(attr_vals, keypad_size.props_per_key)
customer_attr_view = attr_vals.reshape(-1, keypad_size.props_per_key)
attr_keypad_view = list_to_matrix(attr_vals, keypad_size.props_per_key)
attr_set_view = matrix_transpose(attr_keypad_view)
attr_keypad_view = attr_vals.reshape(-1, keypad_size.props_per_key)
attr_set_view = attr_keypad_view.T
set_attribute_dict = dict(zip(set_vals, attr_set_view))
session_id, signup_interface = api.generate_signup_interface(customer_id)
signup_keypad = list_to_matrix(signup_interface, keypad_size.numb_of_keys)
session_id, signup_interface = api.generate_signup_keypad(customer_id)
#signup_keypad = list_to_matrix(signup_interface, keypad_size.numb_of_keys)
signup_keypad = signup_interface.reshape(-1, keypad_size.props_per_key)
username = random_username()
passcode_len = 4
@@ -93,23 +93,22 @@ if __name__ == "__main__":
user_keys = customer.users[username].cipher
padded_passcode_server_set = user_keys.pad_user_mask(passcode_server_set, customer.cipher.set_key)
padded_passcode_server_set = user_keys.pad_user_mask(np.array(passcode_server_set), customer.cipher.set_key)
set_idx = [customer.cipher.get_set_index(set_val) for set_val in padded_passcode_server_set]
mask_set_keys = [user_keys.set_key[idx] for idx in set_idx]
ciphered_mask = xor_lists(mask_set_keys, padded_passcode_server_set)
ciphered_mask = xor_lists(ciphered_mask, user_keys.mask_key)
ciphered_mask = np.bitwise_xor(mask_set_keys, padded_passcode_server_set)
ciphered_mask = np.bitwise_xor(ciphered_mask, user_keys.mask_key)
mask = user_keys.encode_base64_str(ciphered_mask)
ciphered_customer_attrs = xor_lists(customer.cipher.prop_key, user_keys.prop_key)
#ciphered_customer_attrs = xor_lists(customer.cipher.prop_key, user_keys.prop_key)
ciphered_customer_attrs = np.bitwise_xor(customer.cipher.prop_key, user_keys.prop_key)
passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in user_passcode]
pad_len = customer.nkode_policy.max_nkode_len - passcode_len
passcode_ciphered_attrs.extend([0 for _ in range(pad_len)])
ciphered_code = xor_lists(passcode_ciphered_attrs, user_keys.pass_key)
passcode_bytes = int_array_to_bytes(ciphered_code)
#ciphered_code = xor_lists(passcode_ciphered_attrs, user_keys.pass_key)
ciphered_code = np.bitwise_xor(passcode_ciphered_attrs, user_keys.pass_key)
#passcode_bytes = int_array_to_bytes(ciphered_code)
passcode_bytes = ciphered_code.tobytes()
passcode_digest = base64.b64encode(hashlib.sha256(passcode_bytes).digest())
hashed_data = bcrypt.hashpw(passcode_digest, user_keys.salt)
code = hashed_data.decode("utf-8")
@@ -121,7 +120,7 @@ if __name__ == "__main__":
"""
USER LOGIN
"""
login_interface = api.get_login_interface(username, customer_id)
login_interface = api.get_login_keypad(username, customer_id)
login_keypad = list_to_matrix(login_interface, keypad_size.props_per_key)
selected_keys_login = select_keys_with_passcode_values(user_passcode, login_interface, keypad_size.props_per_key)
success = api.login(customer_id, username, selected_keys_login)
@@ -137,11 +136,11 @@ if __name__ == "__main__":
user_keys = user.cipher
user_mask = user.enciphered_passcode.mask
decoded_mask = user_keys.decode_base64_str(user_mask)
deciphered_mask = xor_lists(decoded_mask, user_keys.mask_key)
set_key_rand_component = xor_lists(set_vals, user_keys.set_key)
deciphered_mask = np.bitwise_xor(decoded_mask, user_keys.mask_key)
set_key_rand_component = np.bitwise_xor(set_vals, user_keys.set_key)
login_passcode_sets = []
for set_cipher in deciphered_mask[:passcode_len]:
set_idx = set_key_rand_component.index(set_cipher)
set_idx = np.where(set_key_rand_component == set_cipher)[0][0]
login_passcode_sets.append(set_vals[set_idx])
"""
@@ -154,7 +153,7 @@ if __name__ == "__main__":
for idx in range(passcode_len):
key_numb = selected_keys_login[idx]
set_idx = set_vals_idx[idx]
selected_attr_idx = customer.users[username].user_interface.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)
selected_attr_idx = customer.users[username].user_keypad.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)
presumed_selected_attributes_idx.append(selected_attr_idx)
"""
@@ -166,17 +165,16 @@ if __name__ == "__main__":
customer.cipher.renew()
new_attrs = customer.cipher.prop_key
new_sets = customer.cipher.set_key
customer_new_attr_view = list_to_matrix(new_attrs, keypad_size.props_per_key)
customer_new_attr_view = new_attrs.reshape(-1, keypad_size.props_per_key)
"""
RENEW USER
"""
attrs_xor = xor_lists(new_attrs, old_attrs)
sets_xor = xor_lists(new_sets, old_sets)
attrs_xor = np.bitwise_xor(new_attrs, old_attrs)
sets_xor = np.bitwise_xor(new_sets, old_sets)
for user in customer.users.values():
user.renew = True
user.cipher.set_key = xor_lists(user.cipher.set_key, sets_xor)
user.cipher.prop_key = xor_lists(user.cipher.prop_key, attrs_xor)
user.cipher.set_key = np.bitwise_xor(user.cipher.set_key, sets_xor)
user.cipher.prop_key = np.bitwise_xor(user.cipher.prop_key, attrs_xor)
"""
REFRESH USER KEYS