Object
These two class methods below are safer than using above instance methods combinatorially because an instance will have held a key even if it’s no longer in use.
# File lib/hmac.rb, line 96
96: def Base.digest(key, text)
97: hmac = self.new(key)
98: begin
99: hmac.update(text)
100: hmac.digest
101: ensure
102: hmac.reset_key
103: end
104: end
# File lib/hmac.rb, line 106
106: def Base.hexdigest(key, text)
107: hmac = self.new(key)
108: begin
109: hmac.update(text)
110: hmac.hexdigest
111: ensure
112: hmac.reset_key
113: end
114: end
# File lib/hmac.rb, line 23
23: def initialize(algorithm, block_size, output_length, key)
24: @algorithm = algorithm
25: @block_size = block_size
26: @output_length = output_length
27: @initialized = false
28: @key_xor_ipad = ''
29: @key_xor_opad = ''
30: set_key(key) unless key.nil?
31: end
# File lib/hmac.rb, line 82
82: def digest
83: check_status
84: @md.digest
85: end
# File lib/hmac.rb, line 87
87: def hexdigest
88: check_status
89: @md.hexdigest
90: end
# File lib/hmac.rb, line 59
59: def reset_key
60: @key_xor_ipad.gsub!(/./, '?')
61: @key_xor_opad.gsub!(/./, '?')
62: @key_xor_ipad[0..1] = ''
63: @key_xor_opad[0..1] = ''
64: @initialized = false
65: end
# File lib/hmac.rb, line 42
42: def set_key(key)
43: # If key is longer than the block size, apply hash function
44: # to key and use the result as a real key.
45: key = @algorithm.digest(key) if key.size > @block_size
46: akey = key.unpack("C*")
47: key_xor_ipad = ("\x36" * @block_size).unpack("C*")
48: key_xor_opad = ("\x5C" * @block_size).unpack("C*")
49: for i in 0 .. akey.size - 1
50: key_xor_ipad[i] ^= akey[i]
51: key_xor_opad[i] ^= akey[i]
52: end
53: @key_xor_ipad = key_xor_ipad.pack("C*")
54: @key_xor_opad = key_xor_opad.pack("C*")
55: @md = @algorithm.new
56: @initialized = true
57: end
# File lib/hmac.rb, line 67
67: def update(text)
68: check_status
69: # perform inner H
70: md = @algorithm.new
71: md.update(@key_xor_ipad)
72: md.update(text)
73: str = md.digest
74: # perform outer H
75: md = @algorithm.new
76: md.update(@key_xor_opad)
77: md.update(str)
78: @md = md
79: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.