From 83040f948f6e8264fb427b5d9cf338f46a906fe1 Mon Sep 17 00:00:00 2001 From: John Turner Date: Tue, 23 Sep 2025 21:11:01 -0400 Subject: change keys to 78 bit random tokens and humanize the result Instead of picking random words via a random choice function, we generate a 13 * key_length bit random int as the key. We humanize the key by splitting it up into 13 bit segments and using the 13 bits as an index into the word list. This allows us to store the keys in binary form which is faster and uses less space. --- tests/test_sqlite_storage.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'tests/test_sqlite_storage.py') diff --git a/tests/test_sqlite_storage.py b/tests/test_sqlite_storage.py index 384158c..6a93561 100755 --- a/tests/test_sqlite_storage.py +++ b/tests/test_sqlite_storage.py @@ -6,7 +6,7 @@ import tempfile import aiosqlite import string import random -from pypaste.server import Paste +from pypaste.server import Paste, Key, keygen from pypaste.server.sqlite import Sqlite from datetime import datetime from pathlib import Path @@ -24,11 +24,11 @@ def generate_key() -> str: async def test_exists_but_not_in_our_table(storage: Sqlite) -> None: - key = generate_key() + key = keygen(6) await storage.connection.execute( - "insert into pastes values(?, ?, ?, ?)", - (key, datetime.now().isoformat(), None, bytes()), + "insert into pastes values(?, ?, ?, ?, ?)", + (key.data, key.length, datetime.now().isoformat(), None, bytes()), ) assert not await storage.exists(key) @@ -36,20 +36,20 @@ async def test_exists_but_not_in_our_table(storage: Sqlite) -> None: async def test_exists(storage: Sqlite) -> None: dt = datetime.now() - key = generate_key() + key = keygen(6) - await storage.insert(Paste(key, dt, "test", "hello world")) + await storage.insert(Paste(dt, "test", "hello world"), key) assert await storage.exists(key) - assert not await storage.exists(generate_key()) + assert not await storage.exists(keygen(6)) async def test_delete(storage: Sqlite) -> None: dt = datetime.now() - key = generate_key() + key = keygen(6) - await storage.insert(Paste(key, dt, "test", "hello world")) + await storage.insert(Paste(dt, "test", "hello world"), key) assert await storage.exists(key) @@ -60,14 +60,13 @@ async def test_delete(storage: Sqlite) -> None: async def test_insert_retrieve(storage: Sqlite) -> None: dt = datetime.now() - key = generate_key() + key = keygen(6) - await storage.insert(Paste(key, dt, "test", "hello world")) + await storage.insert(Paste(dt, "test", "hello world"), key) paste = await storage.retrieve(key) assert paste is not None - assert paste.key == key assert paste.dt == dt assert paste.syntax == "test" assert paste.text == "hello world" @@ -81,7 +80,8 @@ async def main() -> int: await connection.execute( ( "create table pastes(" - "key text," + "key blob," + "key_length int," "datetime text," "size int," "syntax text" -- cgit v1.2.3