// Ruby script to verify the HMAC of a file or string.
1
2
3
4
5
6
7
8 require 'openssl'
9 require "getopt/std"
10 include OpenSSL
11 include Digest
12
13 def printhelp
14 help=<<end
15 Purpose: Provides HMAC-SHA1 of a file or string. Text passwords are SHA1 hashed.
16 Usage: hmac.rb ["string to digest"] [-f (pathtofile)] [-k (pathtokeyfile)]
17 Options:
18 -f (pathtofile) digests a file instead of a string
19 -k (pathtokeyfile) does not prompt for key and uses the specified file as a key instead.
20 end
21 puts help
22 exit
23 end
24
25 if ARGV.size < 1
26 printhelp
27 elsif ARGV.size==1
28 @plaintext=ARGV.shift
29 else
30 begin
31 opt = Getopt::Std.getopts("f:k:")
32 rescue Getopt::StdError
33 printhelp
34 end
35
36 if opt["f"]
37 @plaintext=File.read(opt["f"])
38 end
39 if opt["k"]
40 @key=File.read(opt["k"])
41 end
42
43 printhelp if (!@plaintext&&!@key)
44 end
45
46 def getkey
47 return @key if @key
48 print "Please type your key then push enter:"
49 return SHA1.new(gets()).to_s
50 end
51
52 def main
53 hmacd=HMAC.new(getkey(), SHA1.new)
54 hmacd.update(@plaintext)
55 puts hmacd.to_s
56 end
57
58 main