summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoreltrac <hi@eltr.ac>2026-09-11 20:48:03 +0800
committereltrac <hi@eltr.ac>2026-09-11 20:48:03 +0800
commit22c52332f586b5e743ad191b4cae763551bf83ea (patch)
tree47019d7804d2e0576dd0f56290f903d83483d2d5 /src
parente00891a5e7605e03f7cdd150bcfa24e87b2c8e0d (diff)
feed, receiver, wrapper: replace MD5 \w SHA-256
also refactored weepinbell.utils.digest. business logic no longer depend on a concrete algorithm like SHA-256 or MD5, it's just a `digest` function whose defaults can be replaced anytime.
Diffstat (limited to 'src')
-rw-r--r--src/weepinbell/feed.clj4
-rw-r--r--src/weepinbell/receiver.clj4
-rw-r--r--src/weepinbell/utils/digest.clj18
-rw-r--r--src/weepinbell/wrapper.clj6
4 files changed, 22 insertions, 10 deletions
diff --git a/src/weepinbell/feed.clj b/src/weepinbell/feed.clj
index 7f1963d..aa118c5 100644
--- a/src/weepinbell/feed.clj
+++ b/src/weepinbell/feed.clj
@@ -1,14 +1,14 @@
(ns weepinbell.feed
(:require [clj-rss.core :as rss]
[weepinbell.utils.url :refer [get-domain]]
- [weepinbell.utils.digest :refer [md5-hash]])
+ [weepinbell.utils.digest :refer [digest]])
(:import [java.time Instant]))
(defn make-feed-items [wms config]
(->> wms
(map #(let [wm (:webmention %)
id (if-let [id (:id wm)] id
- (md5-hash (str (:source wm) (:target wm))))
+ (digest (str (:source wm) (:target wm))))
permalink (str (:base-url config) "/api/mention/" id)
modified (:modified wm)
pub-date (when modified
diff --git a/src/weepinbell/receiver.clj b/src/weepinbell/receiver.clj
index bf4bce7..b805e43 100644
--- a/src/weepinbell/receiver.clj
+++ b/src/weepinbell/receiver.clj
@@ -3,7 +3,7 @@
(:require [clojure.string :as string]
[clojure.core.async :refer [put!]]
[weepinbell.utils.url :refer [get-domain valid-http-url?]]
- [weepinbell.utils.digest :refer [md5-hash]]))
+ [weepinbell.utils.digest :refer [digest]]))
;; utilities
(defn- remove-anchor
@@ -68,7 +68,7 @@
(put! wmc new-wm)
;; respond with 201 when Webmention passed checks and
;; was sent into queue, include a Location header
- (let [wm-id (md5-hash (str source target))
+ (let [wm-id (digest (str source target))
wm-url (str (:base-url config) "/api/mention/" wm-id)
response {:status 201
:message "Webmention is accepted and pending for verification."
diff --git a/src/weepinbell/utils/digest.clj b/src/weepinbell/utils/digest.clj
index 979c547..4e50709 100644
--- a/src/weepinbell/utils/digest.clj
+++ b/src/weepinbell/utils/digest.clj
@@ -1,7 +1,19 @@
(ns weepinbell.utils.digest
- (:import java.security.MessageDigest))
+ (:import java.security.MessageDigest)
+ (:require [clojure.string :refer [upper-case]]))
-(defn md5-hash [^String s]
- (let [algorithm (MessageDigest/getInstance "MD5")
+(defn- java-digest [^String s algo]
+ (let [algorithm (MessageDigest/getInstance algo)
raw-bytes (.digest algorithm (.getBytes s "UTF-8"))]
(apply str (map #(format "%02x" %) raw-bytes))))
+
+(defn digest
+ ([s]
+ (digest s :sha-256 32))
+ ([s algo]
+ (digest s algo 32))
+ ([s algo length]
+ (->> (java-digest s (upper-case (name algo)))
+ (take length)
+ (apply str))))
+
diff --git a/src/weepinbell/wrapper.clj b/src/weepinbell/wrapper.clj
index a3659e7..e013f6d 100644
--- a/src/weepinbell/wrapper.clj
+++ b/src/weepinbell/wrapper.clj
@@ -4,7 +4,7 @@
download-file]]
[weepinbell.microformats-parser :as mf-parser]
[taoensso.timbre :as timbre]
- [weepinbell.utils.digest :refer [md5-hash]]))
+ [weepinbell.utils.digest :refer [digest]]))
(defn- find-mf-photo [mf]
(or (-> mf
@@ -18,7 +18,7 @@
(if raw-photo
(let [photo-url (str raw-photo)
file-ext (or (re-find #"\.(?:jpg|jpeg|png|gif|webp)" photo-url) ".jpg")
- filename (str (md5-hash photo-url) file-ext)
+ filename (str (digest photo-url) file-ext)
local-path (str "data/avatars/" filename)
public-path (str "/avatars/" filename)]
;; download photo if not exists
@@ -38,7 +38,7 @@
javamap->map)
wm {:source source
:target target
- :id (md5-hash (str source target))
+ :id (digest (str source target))
:modified (System/currentTimeMillis)}
proxied-photo (-> mf
find-mf-photo