From 05226edd09449d826b2d007af471d8e306930b42 Mon Sep 17 00:00:00 2001 From: Donovan Date: Thu, 13 Mar 2025 10:53:46 -0500 Subject: [PATCH] remove utils from nkode_tutorial.ipynb --- docs/render_markdown.py | 2 +- notebooks/nkode_tutorial.ipynb | 370 +++++++++++++++------------------ src/user_keypad.py | 1 - 3 files changed, 172 insertions(+), 201 deletions(-) diff --git a/docs/render_markdown.py b/docs/render_markdown.py index 47581dd..b6fa7f3 100644 --- a/docs/render_markdown.py +++ b/docs/render_markdown.py @@ -71,7 +71,7 @@ if __name__ == "__main__": set_attribute_dict = dict(zip(set_vals, attr_set_view)) session_id, signup_interface = api.generate_signup_keypad(customer_id) - #signup_keypad = list_to_matrix(signup_interface, keypad_size.numb_of_keys) + #signup_keypad = list_to_matrix(signup_keypad, keypad_size.numb_of_keys) signup_keypad = signup_interface.reshape(-1, keypad_size.props_per_key) username = random_username() diff --git a/notebooks/nkode_tutorial.ipynb b/notebooks/nkode_tutorial.ipynb index dea0a83..39bb6dc 100644 --- a/notebooks/nkode_tutorial.ipynb +++ b/notebooks/nkode_tutorial.ipynb @@ -5,19 +5,19 @@ "source": [ "from src.nkode_api import NKodeAPI\n", "from src.models import NKodePolicy, KeypadSize\n", - "from src.utils import list_to_matrix, matrix_transpose\n", "from secrets import choice\n", - "from string import ascii_lowercase" + "from string import ascii_lowercase\n", + "import numpy as np" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:01.727846Z", - "start_time": "2025-03-10T15:54:01.637744Z" + "end_time": "2025-03-13T15:40:54.981894Z", + "start_time": "2025-03-13T15:40:54.975972Z" } }, "outputs": [], - "execution_count": 2 + "execution_count": 157 }, { "cell_type": "code", @@ -26,24 +26,26 @@ " return \"test_username\" + \"\".join([choice(ascii_lowercase) for _ in range(6)])\n", "\n", "\n", - "def select_keys_with_passcode_values(user_passcode: list[int], interface: list[int], attrs_per_key: int) -> list[int]:\n", - " return [interface.index(attr) // attrs_per_key for attr in user_passcode]\n", + "def select_keys_with_passcode_values(user_passcode: list[int], keypad: np.ndarray, attrs_per_key: int) -> list[int]:\n", + " indices = [np.where(keypad == attr)[0][0] for attr in user_passcode]\n", + " return [int(index // attrs_per_key) for index in indices]\n", "\n", - "def keypad_view(interface: list[int], attrs_per_key: int):\n", + "\n", + "def keypad_view(keypad: np.ndarray, attrs_per_key: int):\n", " print(\"Keypad View\")\n", - " interface_keypad = list_to_matrix(interface, attrs_per_key)\n", + " interface_keypad = keypad.reshape(-1, attrs_per_key)\n", " for idx, key_vals in enumerate(interface_keypad):\n", " print(f\"Key {idx}: {key_vals}\")\n" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:01.735895Z", - "start_time": "2025-03-10T15:54:01.733121Z" + "end_time": "2025-03-13T15:41:02.574902Z", + "start_time": "2025-03-13T15:41:02.566577Z" } }, "outputs": [], - "execution_count": 3 + "execution_count": 159 }, { "cell_type": "code", @@ -53,12 +55,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:01.746332Z", - "start_time": "2025-03-10T15:54:01.744308Z" + "end_time": "2025-03-13T15:41:06.702836Z", + "start_time": "2025-03-13T15:41:06.697810Z" } }, "outputs": [], - "execution_count": 4 + "execution_count": 160 }, { "cell_type": "markdown", @@ -98,12 +100,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.001142Z", - "start_time": "2025-03-10T15:54:01.752948Z" + "end_time": "2025-03-13T15:41:09.768933Z", + "start_time": "2025-03-13T15:41:09.760522Z" } }, "outputs": [], - "execution_count": 5 + "execution_count": 161 }, { "cell_type": "markdown", @@ -133,15 +135,15 @@ "attr_vals = customer.cipher.prop_key\n", "print(f\"Customer Sets: {set_vals}\")\n", "print(f\"Customer Attributes:\")\n", - "interface_keypad = list_to_matrix(attr_vals, keypad_size.props_per_key)\n", - "for idx, key_vals in enumerate(interface_keypad):\n", + "customer_prop_keypad = attr_vals.reshape(-1, keypad_size.props_per_key)\n", + "for idx, key_vals in enumerate(customer_prop_keypad):\n", " print(f\"{key_vals}\")" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.011259Z", - "start_time": "2025-03-10T15:54:02.008541Z" + "end_time": "2025-03-13T15:41:11.949505Z", + "start_time": "2025-03-13T15:41:11.941034Z" } }, "outputs": [ @@ -149,17 +151,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer Sets: [5584, 23615, 13665, 42236, 20931, 35557]\n", + "Customer Sets: [25709 8136 44044 46233 29423 25038]\n", "Customer Attributes:\n", - "[51116, 4647, 54248, 42959, 35151, 56238]\n", - "[37806, 51776, 33630, 63761, 13028, 29812]\n", - "[41783, 30499, 23526, 21846, 1217, 40587]\n", - "[20418, 53142, 62008, 29738, 64343, 49564]\n", - "[4306, 19073, 56680, 38208, 21317, 14264]\n" + "[57582 3595 14389 48861 6004 53011]\n", + "[ 2051 49181 8905 9645 63388 60901]\n", + "[ 5952 47243 39367 26329 21337 27385]\n", + "[ 8812 29822 227 12486 4712 16109]\n", + "[28006 29445 47125 35353 35910 6662]\n" ] } ], - "execution_count": 6 + "execution_count": 162 }, { "cell_type": "markdown", @@ -173,9 +175,7 @@ { "cell_type": "code", "source": [ - "attr_keypad_view = list_to_matrix(attr_vals, keypad_size.props_per_key)\n", - "attr_set_view = matrix_transpose(attr_keypad_view)\n", - "set_attribute_dict = dict(zip(set_vals, attr_set_view))\n", + "set_attribute_dict = dict(zip(set_vals, customer_prop_keypad.T))\n", "print(f\"Set to Attribute Map:\")\n", "for set_val, attrs in set_attribute_dict.items():\n", " print(f\"{set_val}: {attrs}\")" @@ -183,8 +183,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:16.867336Z", - "start_time": "2025-03-10T15:54:16.864589Z" + "end_time": "2025-03-13T15:41:14.890303Z", + "start_time": "2025-03-13T15:41:14.883511Z" } }, "outputs": [ @@ -193,16 +193,16 @@ "output_type": "stream", "text": [ "Set to Attribute Map:\n", - "5584: [51116, 37806, 41783, 20418, 4306]\n", - "23615: [4647, 51776, 30499, 53142, 19073]\n", - "13665: [54248, 33630, 23526, 62008, 56680]\n", - "42236: [42959, 63761, 21846, 29738, 38208]\n", - "20931: [35151, 13028, 1217, 64343, 21317]\n", - "35557: [56238, 29812, 40587, 49564, 14264]\n" + "25709: [57582 2051 5952 8812 28006]\n", + "8136: [ 3595 49181 47243 29822 29445]\n", + "44044: [14389 8905 39367 227 47125]\n", + "46233: [48861 9645 26329 12486 35353]\n", + "29423: [ 6004 63388 21337 4712 35910]\n", + "25038: [53011 60901 27385 16109 6662]\n" ] } ], - "execution_count": 8 + "execution_count": 163 }, { "cell_type": "markdown", @@ -226,30 +226,30 @@ { "cell_type": "code", "source": [ - "session_id, signup_interface = api.generate_signup_interface(customer_id)\n", - "list_to_matrix(signup_interface, keypad_size.numb_of_keys)" + "session_id, signup_keypad = api.generate_signup_keypad(customer_id)\n", + "print(signup_keypad.reshape(-1, keypad_size.numb_of_keys))" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:20.741427Z", - "start_time": "2025-03-10T15:54:20.731719Z" + "end_time": "2025-03-13T15:41:19.446496Z", + "start_time": "2025-03-13T15:41:19.440194Z" } }, "outputs": [ { - "ename": "AttributeError", - "evalue": "'NKodeAPI' object has no attribute 'generate_signup_interface'", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mAttributeError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[9], line 1\u001B[0m\n\u001B[0;32m----> 1\u001B[0m session_id, signup_interface \u001B[38;5;241m=\u001B[39m \u001B[43mapi\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mgenerate_signup_interface\u001B[49m(customer_id)\n\u001B[1;32m 2\u001B[0m list_to_matrix(signup_interface, keypad_size\u001B[38;5;241m.\u001B[39mnumb_of_keys)\n", - "\u001B[0;31mAttributeError\u001B[0m: 'NKodeAPI' object has no attribute 'generate_signup_interface'" + "name": "stdout", + "output_type": "stream", + "text": [ + "[[21 3 27 15 9]\n", + " [19 1 25 13 7]\n", + " [23 5 29 17 11]\n", + " [22 4 28 16 10]\n", + " [20 2 26 14 8]]\n" ] } ], - "execution_count": 9 + "execution_count": 164 }, { "cell_type": "markdown", @@ -264,21 +264,21 @@ { "cell_type": "code", "source": [ - "keypad_view(signup_interface, keypad_size.numb_of_keys)\n", + "keypad_view(signup_keypad, keypad_size.numb_of_keys)\n", "username = random_username()\n", "passcode_len = 4\n", - "user_passcode = signup_interface[:passcode_len]\n", - "selected_keys_set = select_keys_with_passcode_values(user_passcode, signup_interface, keypad_size.numb_of_keys)\n", + "user_passcode = signup_keypad[:passcode_len]\n", + "selected_keys_set = select_keys_with_passcode_values(user_passcode, signup_keypad, keypad_size.numb_of_keys)\n", "print(f\"User Passcode: {user_passcode}\")\n", "print(f\"Selected Keys\\n{selected_keys_set}\")\n", - "server_side_attr = [customer.attributes.attr_vals[idx] for idx in user_passcode]\n", + "server_side_attr = [int(customer.cipher.prop_key[idx]) for idx in user_passcode]\n", "print(f\"User Passcode Server-side Attributes: {server_side_attr}\")" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.091787Z", - "start_time": "2025-03-07T19:01:08.471813Z" + "end_time": "2025-03-13T15:41:22.138365Z", + "start_time": "2025-03-13T15:41:22.130412Z" } }, "outputs": [ @@ -287,24 +287,19 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [3, 20, 10, 6, 29]\n", - "Key 1: [27, 26, 4, 24, 11]\n", - "Key 2: [15, 2, 22, 0, 5]\n", - "Key 3: [9, 8, 16, 12, 23]\n", - "Key 4: [21, 14, 28, 18, 17]\n", - "User Passcode: [3, 20, 10, 6]\n", + "Key 0: [21 3 27 15 9]\n", + "Key 1: [19 1 25 13 7]\n", + "Key 2: [23 5 29 17 11]\n", + "Key 3: [22 4 28 16 10]\n", + "Key 4: [20 2 26 14 8]\n", + "User Passcode: [21 3 27 15]\n", "Selected Keys\n", "[0, 0, 0, 0]\n", - "User Passcode Server-side Attributes: [34493, 53473, 4893, 23503]\n" + "User Passcode Server-side Attributes: [12486, 48861, 35353, 26329]\n" ] } ], - "execution_count": 65 - }, - { - "metadata": {}, - "cell_type": "markdown", - "source": "" + "execution_count": 165 }, { "cell_type": "code", @@ -317,8 +312,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.111894Z", - "start_time": "2025-03-07T19:01:08.494090Z" + "end_time": "2025-03-13T15:41:25.094831Z", + "start_time": "2025-03-13T15:41:25.087580Z" } }, "outputs": [ @@ -327,17 +322,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [15, 8, 28, 24, 29]\n", - "Key 1: [27, 20, 22, 12, 17]\n", - "Key 2: [9, 14, 4, 6, 5]\n", - "Key 3: [3, 2, 16, 18, 11]\n", - "Key 4: [21, 26, 10, 0, 23]\n", + "Key 0: [22 2 29 15 7]\n", + "Key 1: [23 4 27 13 8]\n", + "Key 2: [21 5 25 14 10]\n", + "Key 3: [19 3 26 16 11]\n", + "Key 4: [20 1 28 17 9]\n", "Selected Keys\n", - "[3, 1, 4, 2]\n" + "[2, 3, 1, 0]\n" ] } ], - "execution_count": 66 + "execution_count": 166 }, { "cell_type": "code", @@ -349,8 +344,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.115281Z", - "start_time": "2025-03-07T19:01:08.522178Z" + "end_time": "2025-03-13T15:43:12.401745Z", + "start_time": "2025-03-13T15:42:59.685701Z" } }, "outputs": [ @@ -362,7 +357,7 @@ ] } ], - "execution_count": 67 + "execution_count": 168 }, { "metadata": {}, @@ -381,19 +376,17 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.129677Z", - "start_time": "2025-03-10T15:41:40.490919Z" + "end_time": "2025-03-13T15:43:26.496044Z", + "start_time": "2025-03-13T15:43:26.484618Z" } }, "cell_type": "code", "source": [ "from src.user_cipher import UserCipher\n", - "from src.utils import xor_lists\n", - "import numpy as np\n", "\n", "\n", - "set_key = [46785, 4782, 4405, 44408, 35377, 55527]\n", - "set_key = xor_lists(set_key, customer.attributes.set_vals)\n", + "set_key = np.array([46785, 4782, 4405, 44408, 35377, 55527])\n", + "set_key = np.bitwise_xor(set_key, customer.cipher.set_key)\n", "user_keys = UserCipher(\n", " prop_key = np.array([\n", " 57200, 8398, 54694, 25997, 30388,\n", @@ -410,25 +403,22 @@ " max_nkode_len=customer.nkode_policy.max_nkode_len, \n", ")\n", "\n", - "passcode_server_attr = [customer.attributes.attr_vals[idx] for idx in user_passcode]\n", - "passcode_server_set = [customer.attributes.get_attr_set_val(attr) for attr in passcode_server_attr]\n", + "passcode_server_attr = [int(customer.cipher.prop_key[idx]) for idx in user_passcode]\n", + "passcode_server_set = [int(customer.cipher.get_prop_set_val(attr)) for attr in passcode_server_attr]\n", "print(f\"Passcode Set Vals: {passcode_server_set}\")\n", "print(f\"Passcode Attr Vals: {passcode_server_attr}\")" ], "outputs": [ { - "ename": "NameError", - "evalue": "name 'customer' is not defined", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[5], line 6\u001B[0m\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01msrc\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mutils\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m xor_lists\n\u001B[1;32m 5\u001B[0m set_key \u001B[38;5;241m=\u001B[39m [\u001B[38;5;241m46785\u001B[39m, \u001B[38;5;241m4782\u001B[39m, \u001B[38;5;241m4405\u001B[39m, \u001B[38;5;241m44408\u001B[39m, \u001B[38;5;241m35377\u001B[39m, \u001B[38;5;241m55527\u001B[39m]\n\u001B[0;32m----> 6\u001B[0m set_key \u001B[38;5;241m=\u001B[39m xor_lists(set_key, \u001B[43mcustomer\u001B[49m\u001B[38;5;241m.\u001B[39mattributes\u001B[38;5;241m.\u001B[39mset_vals)\n\u001B[1;32m 7\u001B[0m user_keys \u001B[38;5;241m=\u001B[39m UserCipher(\n\u001B[1;32m 8\u001B[0m prop_key \u001B[38;5;241m=\u001B[39m [\n\u001B[1;32m 9\u001B[0m \u001B[38;5;241m57200\u001B[39m, \u001B[38;5;241m8398\u001B[39m, \u001B[38;5;241m54694\u001B[39m, \u001B[38;5;241m25997\u001B[39m, \u001B[38;5;241m30388\u001B[39m,\n\u001B[0;32m (...)\u001B[0m\n\u001B[1;32m 20\u001B[0m max_nkode_len\u001B[38;5;241m=\u001B[39mcustomer\u001B[38;5;241m.\u001B[39mnkode_policy\u001B[38;5;241m.\u001B[39mmax_nkode_len, \n\u001B[1;32m 21\u001B[0m )\n\u001B[1;32m 23\u001B[0m passcode_server_attr \u001B[38;5;241m=\u001B[39m [customer\u001B[38;5;241m.\u001B[39mattributes\u001B[38;5;241m.\u001B[39mattr_vals[idx] \u001B[38;5;28;01mfor\u001B[39;00m idx \u001B[38;5;129;01min\u001B[39;00m user_passcode]\n", - "\u001B[0;31mNameError\u001B[0m: name 'customer' is not defined" + "name": "stdout", + "output_type": "stream", + "text": [ + "Passcode Set Vals: [46233, 46233, 46233, 46233]\n", + "Passcode Attr Vals: [12486, 48861, 35353, 26329]\n" ] } ], - "execution_count": 5 + "execution_count": 169 }, { "metadata": {}, @@ -450,36 +440,22 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.130699Z", - "start_time": "2025-03-10T15:45:03.947109Z" + "end_time": "2025-03-13T15:43:30.557640Z", + "start_time": "2025-03-13T15:43:30.550158Z" } }, "cell_type": "code", "source": [ - "from src.utils import xor_lists\n", + "padded_passcode_server_set = user_keys.pad_user_mask(passcode_server_set, customer.cipher.set_key)\n", "\n", - "padded_passcode_server_set = user_keys.pad_user_mask(passcode_server_set, customer.properites.set_vals)\n", - "\n", - "set_idx = [customer.properites.get_set_index(set_val) for set_val in padded_passcode_server_set]\n", + "set_idx = [customer.cipher.get_set_index(set_val) for set_val in padded_passcode_server_set]\n", "mask_set_keys = [user_keys.set_key[idx] for idx in set_idx]\n", - "ciphered_mask = xor_lists(mask_set_keys, padded_passcode_server_set)\n", - "ciphered_mask = xor_lists(ciphered_mask, user_keys.mask_key)\n", + "ciphered_mask = np.bitwise_xor(mask_set_keys, padded_passcode_server_set)\n", + "ciphered_mask = np.bitwise_xor(ciphered_mask, user_keys.mask_key)\n", "mask = user_keys.encode_base64_str(ciphered_mask)" ], - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'user_keys' is not defined", - "output_type": "error", - "traceback": [ - "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m", - "\u001B[0;31mNameError\u001B[0m Traceback (most recent call last)", - "Cell \u001B[0;32mIn[5], line 3\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01msrc\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mutils\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m xor_lists\n\u001B[0;32m----> 3\u001B[0m padded_passcode_server_set \u001B[38;5;241m=\u001B[39m \u001B[43muser_keys\u001B[49m\u001B[38;5;241m.\u001B[39mpad_user_mask(passcode_server_set, customer\u001B[38;5;241m.\u001B[39mproperites\u001B[38;5;241m.\u001B[39mset_vals)\n\u001B[1;32m 5\u001B[0m set_idx \u001B[38;5;241m=\u001B[39m [customer\u001B[38;5;241m.\u001B[39mproperites\u001B[38;5;241m.\u001B[39mget_set_index(set_val) \u001B[38;5;28;01mfor\u001B[39;00m set_val \u001B[38;5;129;01min\u001B[39;00m padded_passcode_server_set]\n\u001B[1;32m 6\u001B[0m mask_set_keys \u001B[38;5;241m=\u001B[39m [user_keys\u001B[38;5;241m.\u001B[39mset_key[idx] \u001B[38;5;28;01mfor\u001B[39;00m idx \u001B[38;5;129;01min\u001B[39;00m set_idx]\n", - "\u001B[0;31mNameError\u001B[0m: name 'user_keys' is not defined" - ] - } - ], - "execution_count": 5 + "outputs": [], + "execution_count": 170 }, { "metadata": {}, @@ -496,8 +472,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.130948Z", - "start_time": "2025-03-07T19:01:09.236822Z" + "end_time": "2025-03-13T15:43:33.209403Z", + "start_time": "2025-03-13T15:43:32.898812Z" } }, "cell_type": "code", @@ -505,29 +481,29 @@ "import bcrypt\n", "import hashlib\n", "import base64\n", - "from src.utils import int_array_to_bytes\n", "\n", - "ciphered_customer_attrs = xor_lists(customer.attributes.attr_vals, user_keys.prop_key)\n", + "ciphered_customer_attrs = np.bitwise_xor(customer.cipher.prop_key, user_keys.prop_key)\n", "passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in user_passcode]\n", "pad_len = customer.nkode_policy.max_nkode_len - passcode_len\n", "\n", "passcode_ciphered_attrs.extend([0 for _ in range(pad_len)])\n", "\n", - "ciphered_code = xor_lists(passcode_ciphered_attrs, user_keys.pass_key)\n", + "ciphered_code = np.bitwise_xor(passcode_ciphered_attrs, user_keys.pass_key)\n", "\n", - "passcode_bytes = int_array_to_bytes(ciphered_code)\n", + "#passcode_bytes = int_array_to_bytes(ciphered_code)\n", + "passcode_bytes = ciphered_code.tobytes()\n", "passcode_digest = base64.b64encode(hashlib.sha256(passcode_bytes).digest())\n", "hashed_data = bcrypt.hashpw(passcode_digest, user_keys.salt)\n", "code = hashed_data.decode(\"utf-8\")" ], "outputs": [], - "execution_count": 70 + "execution_count": 171 }, { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.141842Z", - "start_time": "2025-03-07T19:01:09.488943Z" + "end_time": "2025-03-13T15:43:35.234325Z", + "start_time": "2025-03-13T15:43:35.229292Z" } }, "cell_type": "code", @@ -540,7 +516,7 @@ ")" ], "outputs": [], - "execution_count": 71 + "execution_count": 172 }, { "cell_type": "markdown", @@ -556,9 +532,9 @@ { "cell_type": "code", "source": [ - "login_interface = api.get_login_interface(username, customer_id)\n", - "keypad_view(login_interface, keypad_size.attrs_per_key)\n", - "selected_keys_login = select_keys_with_passcode_values(user_passcode, login_interface, keypad_size.attrs_per_key)\n", + "login_keypad = api.get_login_keypad(username, customer_id)\n", + "keypad_view(login_keypad, keypad_size.props_per_key)\n", + "selected_keys_login = select_keys_with_passcode_values(user_passcode, login_keypad, keypad_size.props_per_key)\n", "print(f\"Selected Keys: {selected_keys_login}\")\n", "success = api.login(customer_id, username, selected_keys_login)\n", "print(success)" @@ -566,8 +542,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.143871Z", - "start_time": "2025-03-07T19:01:09.500580Z" + "end_time": "2025-03-13T15:43:55.881744Z", + "start_time": "2025-03-13T15:43:37.544889Z" } }, "outputs": [ @@ -576,17 +552,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [24, 25, 8, 15, 4, 29]\n", - "Key 1: [12, 19, 2, 3, 16, 23]\n", - "Key 2: [18, 7, 26, 21, 28, 5]\n", - "Key 3: [6, 1, 14, 9, 10, 11]\n", - "Key 4: [0, 13, 20, 27, 22, 17]\n", - "Selected Keys: [1, 4, 3, 3]\n", + "Key 0: [18 19 20 21 22 23]\n", + "Key 1: [0 1 2 3 4 5]\n", + "Key 2: [24 25 26 27 28 29]\n", + "Key 3: [12 13 14 15 16 17]\n", + "Key 4: [ 6 7 8 9 10 11]\n", + "Selected Keys: [0, 1, 2, 3]\n", "True\n" ] } ], - "execution_count": 72 + "execution_count": 173 }, { "metadata": {}, @@ -618,23 +594,20 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.144830Z", - "start_time": "2025-03-07T19:01:09.772200Z" + "end_time": "2025-03-13T15:44:12.223540Z", + "start_time": "2025-03-13T15:44:12.215204Z" } }, "cell_type": "code", "source": [ - "user = customer.users[username]\n", - "set_vals = customer.attributes.set_vals\n", - "user_keys = user.cipher\n", - "user_mask = user.enciphered_passcode.mask\n", - "decoded_mask = user_keys.decode_base64_str(user_mask)\n", - "deciphered_mask = xor_lists(decoded_mask, user_keys.mask_key)\n", - "set_key_rand_component = xor_lists(set_vals, user_keys.set_key)\n", + "user = api.customers[customer_id].users[username]\n", + "decoded_mask = user.cipher.decode_base64_str(user.enciphered_passcode.mask)\n", + "deciphered_mask = np.bitwise_xor(decoded_mask, user.cipher.mask_key)\n", + "set_key = np.bitwise_xor(customer.cipher.set_key, user.cipher.set_key)\n", "passcode_sets = []\n", "for set_cipher in deciphered_mask[:passcode_len]:\n", - " set_idx = set_key_rand_component.index(set_cipher)\n", - " passcode_sets.append(set_vals[set_idx])\n", + " set_idx = np.where(set_key == set_cipher)[0][0]\n", + " passcode_sets.append(int(customer.cipher.set_key[set_idx]))\n", "print(passcode_sets)" ], "outputs": [ @@ -642,11 +615,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "[39610, 52408, 49828, 14533]\n" + "[46233, 46233, 46233, 46233]\n" ] } ], - "execution_count": 73 + "execution_count": 174 }, { "metadata": {}, @@ -656,22 +629,22 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.145423Z", - "start_time": "2025-03-07T19:01:09.792579Z" + "end_time": "2025-03-13T15:44:41.027416Z", + "start_time": "2025-03-13T15:44:15.077074Z" } }, "cell_type": "code", "source": [ - "set_vals_idx = [customer.attributes.get_set_index(set_val) for set_val in passcode_sets]\n", + "set_vals_idx = [customer.cipher.get_set_index(set_val) for set_val in passcode_sets]\n", "\n", "presumed_selected_attributes_idx = []\n", "for idx in range(passcode_len):\n", " key_numb = selected_keys_login[idx]\n", " set_idx = set_vals_idx[idx]\n", - " selected_attr_idx = customer.users[username].user_interface.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)\n", + " selected_attr_idx = customer.users[username].user_keypad.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)\n", " presumed_selected_attributes_idx.append(selected_attr_idx)\n", "\n", - "print(user_passcode == presumed_selected_attributes_idx)" + "print(user_passcode.tolist() == presumed_selected_attributes_idx)" ], "outputs": [ { @@ -682,7 +655,7 @@ ] } ], - "execution_count": 74 + "execution_count": 175 }, { "metadata": {}, @@ -692,14 +665,13 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.145859Z", - "start_time": "2025-03-07T19:01:09.819860Z" + "end_time": "2025-03-13T15:48:46.158380Z", + "start_time": "2025-03-13T15:48:45.854570Z" } }, "cell_type": "code", "source": [ - "enciphered_nkode = user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.attributes)\n", - "\n", + "enciphered_nkode = user.cipher.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.cipher)\n", "print(enciphered_nkode == user.enciphered_passcode.code)\n" ], "outputs": [ @@ -711,7 +683,7 @@ ] } ], - "execution_count": 75 + "execution_count": 178 }, { "cell_type": "markdown", @@ -729,8 +701,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.146298Z", - "start_time": "2025-03-07T19:01:10.066784Z" + "end_time": "2025-03-13T15:50:03.065695Z", + "start_time": "2025-03-13T15:50:02.452807Z" } }, "cell_type": "code", @@ -744,8 +716,8 @@ "api.renew_attributes(customer_id)\n", "print_user_enciphered_code()\n", "\n", - "login_interface = api.get_login_interface(username, customer_id)\n", - "selected_keys_login = select_keys_with_passcode_values(user_passcode, login_interface, keypad_size.attrs_per_key)\n", + "login_keypad = api.get_login_keypad(username, customer_id)\n", + "selected_keys_login = select_keys_with_passcode_values(user_passcode, login_keypad, keypad_size.props_per_key)\n", "success = api.login(customer_id, username, selected_keys_login)\n", "print(success)\n", "print_user_enciphered_code()" @@ -755,14 +727,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "mask: xYa4LE04KVq0jRcaFG9HA8BZHqo=, code: $2b$12$GdGDlzHY6rt.It6xq.EBIud5PgIWKObJdMms3AaYz1JWK1nf4Jj5G\n", - "mask: xYa4LE04KVq0jRcaFG9HA8BZHqo=, code: $2b$12$GdGDlzHY6rt.It6xq.EBIud5PgIWKObJdMms3AaYz1JWK1nf4Jj5G\n", + "mask: Veawti5Qkgy2y/XtFTZZ7w5oYgA=, code: $2b$12$OHHiqL888FauxiXIVICtouT1xR8iwe79oz63mXkPWKldHoLHPsByy\n", + "mask: Veawti5Qkgy2y/XtFTZZ7w5oYgA=, code: $2b$12$OHHiqL888FauxiXIVICtouT1xR8iwe79oz63mXkPWKldHoLHPsByy\n", "True\n", - "mask: jTZF7KTs3zviTwTt7yqXk7BEK7I=, code: $2b$12$BIJdJ/77MAKNtETz8WSB9uWpJo4.jisH/HsN5o55ez294T1a40rxm\n" + "mask: Sxe3AjutJzGrQLgq/RnfK9G3wHc=, code: $2b$12$.wb5cZjbXJBY6/MQxJIlGu/JyqaF78IM.1n3QP6coxOcKLORU/Io6\n" ] } ], - "execution_count": 76 + "execution_count": 179 }, { "metadata": {}, @@ -776,20 +748,20 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.148090Z", - "start_time": "2025-03-07T19:01:11.153001Z" + "end_time": "2025-03-13T15:52:30.906955Z", + "start_time": "2025-03-13T15:52:30.902577Z" } }, "cell_type": "code", "source": [ - "old_attrs = customer.attributes.attr_vals.copy()\n", - "old_sets = customer.attributes.set_vals.copy()\n", - "customer.attributes.renew()\n", - "new_attrs = customer.attributes.attr_vals\n", - "new_sets = customer.attributes.set_vals" + "old_props = customer.cipher.prop_key.copy()\n", + "old_sets = customer.cipher.set_key.copy()\n", + "customer.cipher.renew()\n", + "new_props = customer.cipher.prop_key\n", + "new_sets = customer.cipher.set_key" ], "outputs": [], - "execution_count": 77 + "execution_count": 181 }, { "metadata": {}, @@ -802,21 +774,21 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.154941Z", - "start_time": "2025-03-07T19:01:11.361690Z" + "end_time": "2025-03-13T15:52:39.513715Z", + "start_time": "2025-03-13T15:52:39.510395Z" } }, "cell_type": "code", "source": [ - "attrs_xor = xor_lists(new_attrs, old_attrs)\n", - "sets_xor = xor_lists(new_sets, old_sets)\n", + "props_xor = np.bitwise_xor(new_props, old_props)\n", + "sets_xor = np.bitwise_xor(new_sets, old_sets)\n", "for user in customer.users.values():\n", " user.renew = True\n", - " user.cipher.set_key = xor_lists(user.cipher.set_key, sets_xor)\n", - " 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)\n", + " user.cipher.prop_key = np.bitwise_xor(user.cipher.prop_key, props_xor)" ], "outputs": [], - "execution_count": 78 + "execution_count": 182 }, { "metadata": {}, @@ -826,22 +798,22 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-10T15:54:02.155673Z", - "start_time": "2025-03-07T19:01:11.369911Z" + "end_time": "2025-03-13T15:53:22.653638Z", + "start_time": "2025-03-13T15:53:22.345351Z" } }, "cell_type": "code", "source": [ "user.cipher = UserCipher.create(\n", - " customer.attributes.keypad_size,\n", - " customer.attributes.set_vals,\n", + " customer.cipher.keypad_size,\n", + " customer.cipher.set_key,\n", " user.cipher.max_nkode_len\n", ")\n", - "user.enciphered_passcode = user.cipher.encipher_nkode(presumed_selected_attributes_idx, customer.attributes)\n", + "user.enciphered_passcode = user.cipher.encipher_nkode(presumed_selected_attributes_idx, customer.cipher)\n", "user.renew = False" ], "outputs": [], - "execution_count": 79 + "execution_count": 186 } ], "metadata": { diff --git a/src/user_keypad.py b/src/user_keypad.py index 434ef21..f668bc2 100644 --- a/src/user_keypad.py +++ b/src/user_keypad.py @@ -1,5 +1,4 @@ from dataclasses import dataclass -from secrets import choice import numpy as np from src.models import KeypadSize