diff --git a/backend/deps.edn b/backend/deps.edn index 650c8a479..a1c4feac5 100644 --- a/backend/deps.edn +++ b/backend/deps.edn @@ -30,6 +30,7 @@ com.zaxxer/HikariCP {:mvn/version "5.0.1"} funcool/datoteka {:mvn/version "3.0.64"} + io.whitfin/siphash {:mvn/version "2.0.0"} buddy/buddy-hashers {:mvn/version "1.8.158"} buddy/buddy-sign {:mvn/version "3.4.333"} diff --git a/backend/src/app/db.clj b/backend/src/app/db.clj index 8785f272e..85dad9b7f 100644 --- a/backend/src/app/db.clj +++ b/backend/src/app/db.clj @@ -27,6 +27,8 @@ com.zaxxer.hikari.HikariConfig com.zaxxer.hikari.HikariDataSource com.zaxxer.hikari.metrics.prometheus.PrometheusMetricsTrackerFactory + io.whitfin.siphash.SipHasher + io.whitfin.siphash.SipHasherContainer java.io.InputStream java.io.OutputStream java.lang.AutoCloseable @@ -431,10 +433,19 @@ ;; --- Locks +(def ^:private siphash-state + (SipHasher/container + (uuid/get-bytes uuid/zero))) + +(defn uuid->hash-code + [o] + (.hash ^SipHasherContainer siphash-state + ^bytes (uuid/get-bytes o))) + (defn- xact-check-param [n] (cond - (uuid? n) (uuid/get-word-high n) + (uuid? n) (uuid->hash-code n) (int? n) n :else (throw (IllegalArgumentException. "uuid or number allowed")))) diff --git a/common/src/app/common/uuid.cljc b/common/src/app/common/uuid.cljc index 1a71256ef..011a50b6e 100644 --- a/common/src/app/common/uuid.cljc +++ b/common/src/app/common/uuid.cljc @@ -12,7 +12,9 @@ #?(:clj [clojure.core :as c]) #?(:cljs [app.common.uuid-impl :as impl]) #?(:cljs [cljs.core :as c])) - #?(:clj (:import java.util.UUID))) + #?(:clj (:import + java.util.UUID + java.nio.ByteBuffer))) (def zero #uuid "00000000-0000-0000-0000-000000000000") @@ -51,3 +53,11 @@ #?(:clj (dm/export impl/get-word-low)) + +#?(:clj + (defn get-bytes + [^UUID o] + (let [buf (ByteBuffer/allocate 16)] + (.putLong buf (.getMostSignificantBits o)) + (.putLong buf (.getLeastSignificantBits o)) + (.array buf))))