Skip to content

Instantly share code, notes, and snippets.

Created December 16, 2013 05:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7982574 to your computer and use it in GitHub Desktop.
Save anonymous/7982574 to your computer and use it in GitHub Desktop.
(ns hmac
(:import (javax.crypto Mac) (javax.crypto.spec SecretKeySpec))
)
(defn secret-key-inst [key mac]
(SecretKeySpec. (.getBytes key) (.getAlgorithm mac))
)
(defn sign[key string]
"Returns the signature of a string with a given key, using a SHA-256 HMAC."
(let [mac (Mac/getInstance "HMACSHA256")
secret-key (secret-key-inst key mac)]
(-> (doto mac
(.init secret-key)
(.update (.getBytes string)))
.doFinal)
)
)
(println
(apply str (map #(format "%x" %) (sign "key" "message")))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment