#!/usr/bin/env ruby # # Created by Jon (exabrial+hmacruby@gmail.com) on 2006-11-04. # Copyright (c) 2006. All rights reserved. # Released under MIT License require 'openssl' require "getopt/std" include OpenSSL include Digest def printhelp help=<<end Purpose: Provides HMAC-SHA1 of a file or string. Text passwords are SHA1 hashed. Usage: hmac.rb ["string to digest"] [-f (pathtofile)] [-k (pathtokeyfile)] Options: -f (pathtofile) digests a file instead of a string -k (pathtokeyfile) does not prompt for key and uses the specified file as a key instead. end puts help exit end if ARGV.size < 1 printhelp elsif ARGV.size==1 @plaintext=ARGV.shift else begin opt = Getopt::Std.getopts("f:k:") rescue Getopt::StdError printhelp end if opt["f"] @plaintext=File.read(opt["f"]) end if opt["k"] @key=File.read(opt["k"]) end printhelp if (!@plaintext&&!@key) end def getkey return @key if @key print "Please type your key then push enter:" return SHA1.new(gets()).to_s end def main hmacd=HMAC.new(getkey(), SHA1.new) hmacd.update(@plaintext) puts hmacd.to_s end main
You need to create an account or log in to post comments to this site.