Skip to content

Instantly share code, notes, and snippets.

@jwilkins
Created October 9, 2012 19:27
Show Gist options
  • Save jwilkins/3860887 to your computer and use it in GitHub Desktop.
Save jwilkins/3860887 to your computer and use it in GitHub Desktop.
HMAC CLI (HMAC-SHA1 HMAC-MD5 HMAC-SHA256 HMAC-SHA512)
#!/usr/bin/env ruby
# echo -n 'asdfasdf' |./hmacsha1 --key foobar'
# 60ff950bea9d6dc7018de881909057e4bba14e26
require 'openssl'
require 'optparse'
key = 'horriblekeychangeme'
algo = 'sha512'
verbose = false
OptionParser.new do |options|
options.on("-a", "--algo ALGO",
"Specify algorithm [md5, sha1, sha256, sha512]") do |ha|
# XXX: unless algo in ..
algo = ha
end
options.on("-k", "--key KEY", "Specify HMAC Key") do |hk|
key = hk
end
options.on("-v", "--verbose", "Extra output") do
verbose = true
end
end.parse!
data = ARGF.read
if verbose
puts "algorithm is #{algo}"
puts "key is #{key}"
puts "data is #{data}"
end
puts OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(algo), key, data).unpack("H*")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment