Skip to content

Instantly share code, notes, and snippets.

@bpringe
Last active October 19, 2018 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpringe/9e4602b8cfd5f768d63b06601723d4f2 to your computer and use it in GitHub Desktop.
Save bpringe/9e4602b8cfd5f768d63b06601723d4f2 to your computer and use it in GitHub Desktop.
Calcuate HMAC signature. Substitute `algorithm` for other types. This worked for me for validating Facebook webhook payloads.
;; Make sure you import javax.crypto.Mac and javax.crypto.spec.SecretKeySpec
(defn get-signature
[message secret]
(let [algorithm "HmacSHA1"
signing-key (SecretKeySpec. (.getBytes secret) algorithm)
mac (doto
(Mac/getInstance algorithm)
(.init signing-key))
bytes->hex-str #(apply str (for [b %] (format "%02x" b)))]
(->> (.getBytes message)
(.doFinal mac)
bytes->hex-str)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment