From 88b533c27bb57e025572cf844ed8b8836315ac57 Mon Sep 17 00:00:00 2001 From: Donovan Date: Fri, 7 Mar 2025 13:04:10 -0600 Subject: [PATCH] refactor loose files --- render_markdown.py => docs/render_markdown.py | 4 +- .../dispersion_tutorial.ipynb | 0 .../nkode_calculation.ipynb | 0 .../nkode_tutorial.ipynb | 254 ++++++++++-------- nkode_api.py => src/nkode_api.py | 0 test/test_nkode_api.py | 2 +- 6 files changed, 152 insertions(+), 108 deletions(-) rename render_markdown.py => docs/render_markdown.py (99%) rename dispersion_tutorial.ipynb => notebooks/dispersion_tutorial.ipynb (100%) rename nkode_calculation.ipynb => notebooks/nkode_calculation.ipynb (100%) rename nkode_tutorial.ipynb => notebooks/nkode_tutorial.ipynb (79%) rename nkode_api.py => src/nkode_api.py (100%) diff --git a/render_markdown.py b/docs/render_markdown.py similarity index 99% rename from render_markdown.py rename to docs/render_markdown.py index c41b0eb..5b61aaa 100644 --- a/render_markdown.py +++ b/docs/render_markdown.py @@ -1,6 +1,6 @@ from jinja2 import Environment, FileSystemLoader import os -from nkode_api import NKodeAPI +from src.nkode_api import NKodeAPI from src.models import NKodePolicy, KeypadSize, EncipheredNKode from src.user_cipher_keys import UserCipherKeys from src.utils import list_to_matrix, matrix_transpose, xor_lists @@ -29,7 +29,7 @@ def keypad_view(interface: list[int], attrs_per_key: int): def render_nkode_authentication(data: dict): # Set up the Jinja2 environment and template loader - file_loader = FileSystemLoader('./docs') + file_loader = FileSystemLoader('') env = Environment(loader=file_loader) # Load the template diff --git a/dispersion_tutorial.ipynb b/notebooks/dispersion_tutorial.ipynb similarity index 100% rename from dispersion_tutorial.ipynb rename to notebooks/dispersion_tutorial.ipynb diff --git a/nkode_calculation.ipynb b/notebooks/nkode_calculation.ipynb similarity index 100% rename from nkode_calculation.ipynb rename to notebooks/nkode_calculation.ipynb diff --git a/nkode_tutorial.ipynb b/notebooks/nkode_tutorial.ipynb similarity index 79% rename from nkode_tutorial.ipynb rename to notebooks/nkode_tutorial.ipynb index b083088..e2d9abc 100644 --- a/nkode_tutorial.ipynb +++ b/notebooks/nkode_tutorial.ipynb @@ -3,7 +3,7 @@ { "cell_type": "code", "source": [ - "from nkode_api import NKodeAPI\n", + "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", @@ -12,12 +12,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.475149Z", - "start_time": "2024-08-05T17:40:17.473025Z" + "end_time": "2025-03-07T19:01:08.154404Z", + "start_time": "2025-03-07T19:01:08.149432Z" } }, "outputs": [], - "execution_count": 12 + "execution_count": 58 }, { "cell_type": "code", @@ -38,12 +38,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.556128Z", - "start_time": "2024-08-05T17:40:17.552985Z" + "end_time": "2025-03-07T19:01:08.165511Z", + "start_time": "2025-03-07T19:01:08.162339Z" } }, "outputs": [], - "execution_count": 13 + "execution_count": 59 }, { "cell_type": "code", @@ -53,12 +53,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.559851Z", - "start_time": "2024-08-05T17:40:17.557794Z" + "end_time": "2025-03-07T19:01:08.174320Z", + "start_time": "2025-03-07T19:01:08.172724Z" } }, "outputs": [], - "execution_count": 14 + "execution_count": 60 }, { "cell_type": "markdown", @@ -98,12 +98,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.740656Z", - "start_time": "2024-08-05T17:40:17.570041Z" + "end_time": "2025-03-07T19:01:08.378578Z", + "start_time": "2025-03-07T19:01:08.188114Z" } }, "outputs": [], - "execution_count": 15 + "execution_count": 61 }, { "cell_type": "markdown", @@ -140,8 +140,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.744240Z", - "start_time": "2024-08-05T17:40:17.741773Z" + "end_time": "2025-03-07T19:01:08.387613Z", + "start_time": "2025-03-07T19:01:08.385501Z" } }, "outputs": [ @@ -149,17 +149,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer Sets: [41463, 23817, 10115, 59621, 17701, 39232]\n", + "Customer Sets: [14533, 13441, 52408, 39610, 49828, 11300]\n", "Customer Attributes:\n", - "[15101, 42072, 1835, 15413, 46991, 41978]\n", - "[47133, 25183, 43097, 17581, 20893, 52753]\n", - "[14863, 31428, 41789, 18633, 3962, 50372]\n", - "[53357, 51786, 31149, 59745, 30907, 16058]\n", - "[60389, 36053, 8246, 53494, 23730, 15317]\n" + "[50415, 3350, 62907, 34493, 46982, 55292]\n", + "[23503, 57678, 17001, 8963, 4893, 53685]\n", + "[25912, 9324, 2770, 57761, 57056, 5837]\n", + "[49831, 16518, 53473, 35853, 12433, 20763]\n", + "[60930, 19614, 2083, 2879, 58781, 13705]\n" ] } ], - "execution_count": 16 + "execution_count": 62 }, { "cell_type": "markdown", @@ -183,8 +183,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.747131Z", - "start_time": "2024-08-05T17:40:17.744937Z" + "end_time": "2025-03-07T19:01:08.412394Z", + "start_time": "2025-03-07T19:01:08.410114Z" } }, "outputs": [ @@ -193,16 +193,16 @@ "output_type": "stream", "text": [ "Set to Attribute Map:\n", - "41463: [15101, 47133, 14863, 53357, 60389]\n", - "23817: [42072, 25183, 31428, 51786, 36053]\n", - "10115: [1835, 43097, 41789, 31149, 8246]\n", - "59621: [15413, 17581, 18633, 59745, 53494]\n", - "17701: [46991, 20893, 3962, 30907, 23730]\n", - "39232: [41978, 52753, 50372, 16058, 15317]\n" + "14533: [50415, 23503, 25912, 49831, 60930]\n", + "13441: [3350, 57678, 9324, 16518, 19614]\n", + "52408: [62907, 17001, 2770, 53473, 2083]\n", + "39610: [34493, 8963, 57761, 35853, 2879]\n", + "49828: [46982, 4893, 57056, 12433, 58781]\n", + "11300: [55292, 53685, 5837, 20763, 13705]\n" ] } ], - "execution_count": 17 + "execution_count": 63 }, { "cell_type": "markdown", @@ -232,27 +232,27 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.751757Z", - "start_time": "2024-08-05T17:40:17.748531Z" + "end_time": "2025-03-07T19:01:08.439639Z", + "start_time": "2025-03-07T19:01:08.436629Z" } }, "outputs": [ { "data": { "text/plain": [ - "[[29, 3, 19, 12, 26],\n", - " [5, 27, 1, 0, 8],\n", - " [23, 9, 13, 24, 20],\n", - " [11, 21, 25, 18, 14],\n", - " [17, 15, 7, 6, 2]]" + "[[3, 20, 10, 6, 29],\n", + " [27, 26, 4, 24, 11],\n", + " [15, 2, 22, 0, 5],\n", + " [9, 8, 16, 12, 23],\n", + " [21, 14, 28, 18, 17]]" ] }, - "execution_count": 18, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 18 + "execution_count": 64 }, { "cell_type": "markdown", @@ -280,8 +280,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.755453Z", - "start_time": "2024-08-05T17:40:17.752507Z" + "end_time": "2025-03-07T19:01:08.474579Z", + "start_time": "2025-03-07T19:01:08.471813Z" } }, "outputs": [ @@ -290,19 +290,19 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [29, 3, 19, 12, 26]\n", - "Key 1: [5, 27, 1, 0, 8]\n", - "Key 2: [23, 9, 13, 24, 20]\n", - "Key 3: [11, 21, 25, 18, 14]\n", - "Key 4: [17, 15, 7, 6, 2]\n", - "User Passcode: [29, 3, 19, 12]\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", "Selected Keys\n", "[0, 0, 0, 0]\n", - "User Passcode Server-side Attributes: [15317, 15413, 51786, 14863]\n" + "User Passcode Server-side Attributes: [34493, 53473, 4893, 23503]\n" ] } ], - "execution_count": 19 + "execution_count": 65 }, { "metadata": {}, @@ -320,8 +320,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:17.760514Z", - "start_time": "2024-08-05T17:40:17.758171Z" + "end_time": "2025-03-07T19:01:08.496620Z", + "start_time": "2025-03-07T19:01:08.494090Z" } }, "outputs": [ @@ -330,17 +330,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [23, 3, 25, 6, 8]\n", - "Key 1: [17, 9, 19, 0, 14]\n", - "Key 2: [5, 15, 13, 18, 26]\n", - "Key 3: [11, 27, 7, 12, 20]\n", - "Key 4: [29, 21, 1, 24, 2]\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", "Selected Keys\n", - "[4, 0, 1, 3]\n" + "[3, 1, 4, 2]\n" ] } ], - "execution_count": 20 + "execution_count": 66 }, { "cell_type": "code", @@ -352,8 +352,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:40:18.401946Z", - "start_time": "2024-08-05T17:40:17.761154Z" + "end_time": "2025-03-07T19:01:09.173784Z", + "start_time": "2025-03-07T19:01:08.522178Z" } }, "outputs": [ @@ -365,7 +365,7 @@ ] } ], - "execution_count": 21 + "execution_count": 67 }, { "metadata": {}, @@ -384,8 +384,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-05T17:54:37.484873Z", - "start_time": "2024-08-05T17:54:37.480227Z" + "end_time": "2025-03-07T19:01:09.193871Z", + "start_time": "2025-03-07T19:01:09.190856Z" } }, "cell_type": "code", @@ -422,12 +422,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "Passcode Set Vals: [17701, 39232, 10115, 23817]\n", - "Passcode Attr Vals: [20893, 16058, 41789, 25183]\n" + "Passcode Set Vals: [39610, 52408, 49828, 14533]\n", + "Passcode Attr Vals: [34493, 53473, 4893, 23503]\n" ] } ], - "execution_count": 36 + "execution_count": 68 }, { "metadata": {}, @@ -449,8 +449,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-05T17:52:29.553010Z", - "start_time": "2024-08-05T17:52:29.549861Z" + "end_time": "2025-03-07T19:01:09.223843Z", + "start_time": "2025-03-07T19:01:09.221595Z" } }, "cell_type": "code", @@ -466,7 +466,7 @@ "mask = user_keys.encode_base64_str(ciphered_mask)" ], "outputs": [], - "execution_count": 28 + "execution_count": 69 }, { "metadata": {}, @@ -483,8 +483,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-05T17:52:34.104235Z", - "start_time": "2024-08-05T17:52:33.794884Z" + "end_time": "2025-03-07T19:01:09.479816Z", + "start_time": "2025-03-07T19:01:09.236822Z" } }, "cell_type": "code", @@ -495,7 +495,7 @@ "from src.utils import int_array_to_bytes\n", "\n", "ciphered_customer_attrs = xor_lists(customer.attributes.attr_vals, user_keys.alpha_key)\n", - "passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in passcode]\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", @@ -508,13 +508,13 @@ "code = hashed_data.decode(\"utf-8\")" ], "outputs": [], - "execution_count": 29 + "execution_count": 70 }, { "metadata": { "ExecuteTime": { - "end_time": "2024-08-05T17:52:34.205433Z", - "start_time": "2024-08-05T17:52:34.203278Z" + "end_time": "2025-03-07T19:01:09.495420Z", + "start_time": "2025-03-07T19:01:09.488943Z" } }, "cell_type": "code", @@ -527,7 +527,7 @@ ")" ], "outputs": [], - "execution_count": 30 + "execution_count": 71 }, { "cell_type": "markdown", @@ -553,8 +553,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2024-08-05T17:52:38.297363Z", - "start_time": "2024-08-05T17:52:37.990052Z" + "end_time": "2025-03-07T19:01:09.736403Z", + "start_time": "2025-03-07T19:01:09.500580Z" } }, "outputs": [ @@ -563,17 +563,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [6, 19, 14, 15, 4, 17]\n", - "Key 1: [24, 13, 26, 3, 28, 23]\n", - "Key 2: [0, 7, 2, 9, 16, 5]\n", - "Key 3: [18, 1, 8, 27, 22, 11]\n", - "Key 4: [12, 25, 20, 21, 10, 29]\n", - "Selected Keys: [4, 1, 0, 4]\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", "True\n" ] } ], - "execution_count": 31 + "execution_count": 72 }, { "metadata": {}, @@ -605,8 +605,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-05T17:59:58.753205Z", - "start_time": "2024-08-05T17:59:58.749714Z" + "end_time": "2025-03-07T19:01:09.774425Z", + "start_time": "2025-03-07T19:01:09.772200Z" } }, "cell_type": "code", @@ -629,11 +629,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "[39232, 59621, 23817, 41463]\n" + "[39610, 52408, 49828, 14533]\n" ] } ], - "execution_count": 38 + "execution_count": 73 }, { "metadata": {}, @@ -643,8 +643,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2024-08-05T17:54:03.252644Z", - "start_time": "2024-08-05T17:54:03.249271Z" + "end_time": "2025-03-07T19:01:09.795190Z", + "start_time": "2025-03-07T19:01:09.792579Z" } }, "cell_type": "code", @@ -669,7 +669,7 @@ ] } ], - "execution_count": 35 + "execution_count": 74 }, { "metadata": {}, @@ -677,15 +677,28 @@ "source": "### Compare Enciphered Passcodes" }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-03-07T19:01:10.057899Z", + "start_time": "2025-03-07T19:01:09.819860Z" + } + }, "cell_type": "code", "source": [ "enciphered_nkode = user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, customer.attributes)\n", "\n", "print(enciphered_nkode == user.enciphered_passcode.code)\n" ], - "outputs": [], - "execution_count": null + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "execution_count": 75 }, { "cell_type": "markdown", @@ -701,7 +714,12 @@ } }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-03-07T19:01:11.147274Z", + "start_time": "2025-03-07T19:01:10.066784Z" + } + }, "cell_type": "code", "source": [ "def print_user_enciphered_code():\n", @@ -719,8 +737,19 @@ "print(success)\n", "print_user_enciphered_code()" ], - "outputs": [], - "execution_count": null + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mask: xYa4LE04KVq0jRcaFG9HA8BZHqo=, code: $2b$12$GdGDlzHY6rt.It6xq.EBIud5PgIWKObJdMms3AaYz1JWK1nf4Jj5G\n", + "mask: xYa4LE04KVq0jRcaFG9HA8BZHqo=, code: $2b$12$GdGDlzHY6rt.It6xq.EBIud5PgIWKObJdMms3AaYz1JWK1nf4Jj5G\n", + "True\n", + "mask: jTZF7KTs3zviTwTt7yqXk7BEK7I=, code: $2b$12$BIJdJ/77MAKNtETz8WSB9uWpJo4.jisH/HsN5o55ez294T1a40rxm\n" + ] + } + ], + "execution_count": 76 }, { "metadata": {}, @@ -732,7 +761,12 @@ ] }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-03-07T19:01:11.355505Z", + "start_time": "2025-03-07T19:01:11.153001Z" + } + }, "cell_type": "code", "source": [ "old_attrs = customer.attributes.attr_vals.copy()\n", @@ -742,7 +776,7 @@ "new_sets = customer.attributes.set_vals" ], "outputs": [], - "execution_count": null + "execution_count": 77 }, { "metadata": {}, @@ -753,7 +787,12 @@ ] }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-03-07T19:01:11.363643Z", + "start_time": "2025-03-07T19:01:11.361690Z" + } + }, "cell_type": "code", "source": [ "attrs_xor = xor_lists(new_attrs, old_attrs)\n", @@ -764,7 +803,7 @@ " user.user_keys.alpha_key = xor_lists(user.user_keys.alpha_key, attrs_xor)" ], "outputs": [], - "execution_count": null + "execution_count": 78 }, { "metadata": {}, @@ -772,7 +811,12 @@ "source": "### Refresh User Keys" }, { - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2025-03-07T19:01:11.989265Z", + "start_time": "2025-03-07T19:01:11.369911Z" + } + }, "cell_type": "code", "source": [ "user.user_keys = UserCipherKeys.new(\n", @@ -784,7 +828,7 @@ "user.renew = False" ], "outputs": [], - "execution_count": null + "execution_count": 79 } ], "metadata": { diff --git a/nkode_api.py b/src/nkode_api.py similarity index 100% rename from nkode_api.py rename to src/nkode_api.py diff --git a/test/test_nkode_api.py b/test/test_nkode_api.py index 7720beb..186438c 100644 --- a/test/test_nkode_api.py +++ b/test/test_nkode_api.py @@ -1,5 +1,5 @@ import pytest -from nkode_api import NKodeAPI +from src.nkode_api import NKodeAPI from src.models import NKodePolicy, KeypadSize