Skip to content

Instantly share code, notes, and snippets.

@visibletrap
Last active December 11, 2015 08:08
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 visibletrap/4571244 to your computer and use it in GitHub Desktop.
Save visibletrap/4571244 to your computer and use it in GitHub Desktop.
(defn hex-hmac-sha1 [key input]
(let [secret (javax.crypto.spec.SecretKeySpec. (. key getBytes "UTF-8") "HmacSHA1")
hmac-sha1 (doto (javax.crypto.Mac/getInstance "HmacSHA1") (.init secret))
bytes (. hmac-sha1 doFinal (. input getBytes "UTF-8"))
hex (apply str (map (partial format "%02x") bytes))]
hex))
(hex-hmac-sha1 "secret-key" "string-to-sign")
; => "0b6afb06cd51b2713cf18d99f7f0228d06effd46"
require 'openssl'
sha1 = OpenSSL::Digest::Digest.new('sha1')
OpenSSL::HMAC.hexdigest(sha1, 'secret-key', 'string-to-sign')
# => "0b6afb06cd51b2713cf18d99f7f0228d06effd46"
@hybridknight
Copy link

require('crypto').createHmac('sha1', 'secret-key').update('string-to-sign').digest('hex')
// => '0b6afb06cd51b2713cf18d99f7f0228d06effd46'

@llun
Copy link

llun commented Jan 19, 2013

@hybridknight lua สั้นลงได้อีกนิดนึงนะ :P

require("crypto").hmac.digest("sha1", "string-to-sign", "secret-key")

// => '0b6afb06cd51b2713cf18d99f7f0228d06effd46'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment