aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-03-15 19:55:10 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-03-24 20:25:01 +0000
commita74d1baf0c3b290497936179cba79709f50e9c71 (patch)
treeabca8e07f3b59457e5d34b8ba21570d29543d37c
parent46a7594340cdf5572d60ae9440d36635f7589ecf (diff)
downloadpaste-a74d1baf0c3b290497936179cba79709f50e9c71.tar.gz
paste-a74d1baf0c3b290497936179cba79709f50e9c71.tar.xz
paste-a74d1baf0c3b290497936179cba79709f50e9c71.zip
Add a DATA_HASH UDF (same as SHA256)
This means that the underlying hash function can be changed without needing to change the name. Although the change would still break the database backwards compatibility, but that's a separate problem.
-rw-r--r--paste/db.py13
-rw-r--r--paste/store.py8
2 files changed, 15 insertions, 6 deletions
diff --git a/paste/db.py b/paste/db.py
index bef3c46..f1071e8 100644
--- a/paste/db.py
+++ b/paste/db.py
@@ -7,7 +7,7 @@ from typing import Optional, Union
migrations = [
"""CREATE TABLE file (
- hash BLOB UNIQUE GENERATED ALWAYS AS (sha256(content)) STORED NOT NULL,
+ hash BLOB UNIQUE GENERATED ALWAYS AS (DATA_HASH(content)) STORED NOT NULL,
content BLOB NOT NULL,
created_at INTEGER DEFAULT (unixepoch('now'))
) STRICT;
@@ -15,7 +15,7 @@ migrations = [
CREATE UNIQUE INDEX file_hash_ix ON file ( hash );
CREATE TABLE link (
- name_hash BLOB UNIQUE GENERATED ALWAYS AS (sha256(name)) STORED NOT NULL,
+ name_hash BLOB UNIQUE GENERATED ALWAYS AS (DATA_HASH(name)) STORED NOT NULL,
name TEXT NOT NULL,
content_type TEXT NOT NULL DEFAULT "text/plain",
file_hash BLOB NOT NULL,
@@ -38,6 +38,12 @@ def _sha256_udf(b: Union[bytes, str]) -> bytes:
return sha256(b).digest()
+def _data_hash_udf(b: Union[bytes, str]) -> bytes:
+ if isinstance(b, str):
+ b = b.encode()
+ return sha256(b).digest()
+
+
def get_version(conn: sqlite3.Connection) -> int:
(user_version,) = conn.execute("PRAGMA user_version").fetchone()
return user_version
@@ -71,6 +77,9 @@ def connect(
conn.execute("PRAGMA journal_mode = WAL")
conn.row_factory = sqlite3.Row
conn.create_function(name="sha256", narg=1, func=_sha256_udf, deterministic=True)
+ conn.create_function(
+ name="DATA_HASH", narg=1, func=_data_hash_udf, deterministic=True
+ )
migrate(conn, migrations)
try:
yield conn
diff --git a/paste/store.py b/paste/store.py
index bae578f..26643cf 100644
--- a/paste/store.py
+++ b/paste/store.py
@@ -11,7 +11,7 @@ def put(conn: Connection, name: str, content: bytes, content_type: str):
"""
INSERT INTO link (
name, content_type, file_hash
- ) VALUES (?, ?, sha256(?))
+ ) VALUES (?, ?, DATA_HASH(?))
ON CONFLICT DO UPDATE
SET
content_type = excluded.content_type,
@@ -25,7 +25,7 @@ def get(conn: Connection, name: str):
"""SELECT link.content_type, file.hash, file.content
FROM link
JOIN file ON file.hash = link.file_hash
- WHERE name_hash = sha256(?)""",
+ WHERE name_hash = DATA_HASH(?)""",
(name,),
).fetchone()
return row
@@ -41,7 +41,7 @@ def head(conn: Connection, name: str):
END
FROM link
JOIN file ON file.hash = link.file_hash
- WHERE name_hash = sha256(?)""",
+ WHERE name_hash = DATA_HASH(?)""",
(name,),
).fetchone()
return row
@@ -49,4 +49,4 @@ def head(conn: Connection, name: str):
def delete(conn: Connection, name: str):
with conn:
- conn.execute("DELETE FROM link WHERE name_hash = sha256(?)", (name,))
+ conn.execute("DELETE FROM link WHERE name_hash = DATA_HASH(?)", (name,))