Skip to content

Instantly share code, notes, and snippets.

@dexterous
Created May 6, 2025 20:34
Show Gist options
  • Save dexterous/37b98c7e17e81bbe133b855f0070e830 to your computer and use it in GitHub Desktop.
Save dexterous/37b98c7e17e81bbe133b855f0070e830 to your computer and use it in GitHub Desktop.
;; From https://github.com/ring-clojure/ring/blob/a915a44/ring-core/src/ring/middleware/session/cookie.clj#L29
(defn- hmac
"Generates a Base64 HMAC with the supplied key on a string of data."
[key data]
(let [mac (Mac/getInstance hmac-algorithm)]
(.init mac (SecretKeySpec. key hmac-algorithm))
(codec/base64-encode (.doFinal mac data))))
;; I would prefer to write it like this.
(defn- hmac
"Generates a Base64 HMAC with the supplied key on a string of data."
[key data]
(codec/base64-encode
(-> (doto (Mac/getInstance hmac-algorithm)
(.init (SecretKeySpec. key hmac-algorithm)))
(.doFinal data))))
;; Expands as follows
(pprint (clojure.walk/macroexpand-all
'(defn- hmac
"Generates a Base64 HMAC with the supplied key on a string of data."
[key data]
(codec/base64-encode
(-> (doto (Mac/getInstance hmac-algorithm)
(.init (SecretKeySpec. key hmac-algorithm)))
(.doFinal data))))))
(def hmac
(fn*
([key data]
(codec/base64-encode
(.
(let*
[G__136 (Mac/getInstance hmac-algorithm)]
(. G__136 init (new SecretKeySpec key hmac-algorithm))
G__136)
doFinal
data)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment