Skip to content

Instantly share code, notes, and snippets.

@enriclluelles
Created December 18, 2014 17:59
Show Gist options
  • Save enriclluelles/12927db9d03bee833520 to your computer and use it in GitHub Desktop.
Save enriclluelles/12927db9d03bee833520 to your computer and use it in GitHub Desktop.
Efficient way of getting a HMACSHA256 payload out of a string and a secret in clojure
(defn hmacSHA256
(memoize (fn [payload secret]
(let [mac (Mac/getInstance "HMACSHA256")
secretKey (SecretKeySpec. (.getBytes secret) (.getAlgorithm mac))
byteResult (-> (doto mac
(.init secretKey))
(.doFinal (.getBytes payload)))
stringResult (->> byteResult
(map #(format "%x" %))
(apply str))]
stringResult))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment