Skip to content

Instantly share code, notes, and snippets.

@MSch
Created December 7, 2011 09:44
Show Gist options
  • Save MSch/1442194 to your computer and use it in GitHub Desktop.
Save MSch/1442194 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'digest/sha1'
require 'io/console'
ALPHABET = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
def hmac(key, msg)
split_at = (key.length / 2).floor
key1 = key[0, split_at]
key2 = key[split_at..-1]
hmac = Digest::SHA1.digest(key1 + Digest::SHA1.digest(key2 + msg))
hmac.unpack('C*').map { |c| ALPHABET[c.modulo(ALPHABET.length)] }.join
end
def main
print "Message: "
msg = STDIN.gets.chomp
print "Key: "
STDIN.echo = false
key = STDIN.gets.chomp
STDIN.echo = true
puts
puts "Fingerprint: #{hmac(key,key)}"
puts "HMAC: #{hmac(key, msg)}"
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment