Skip to content

Instantly share code, notes, and snippets.

@visibletrap

visibletrap/hmac-sha1.clj

Last active Dec 11, 2015
Embed
What would you like to do?
(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

This comment has been minimized.

Copy link

@hybridknight hybridknight commented Jan 19, 2013

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

@llun

This comment has been minimized.

Copy link

@llun 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
You can’t perform that action at this time.