Created
December 16, 2013 05:06
-
-
Save anonymous/7982574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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