class HMAC::Base
Private Class Methods
digest(key, text)
click to toggle source
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/hmac.rb, line 88 def Base.digest(key, text) begin hmac = self.new(key) hmac.update(text) hmac.digest ensure hmac.reset_key end end
hexdigest(key, text)
click to toggle source
# File lib/hmac/hmac.rb, line 98 def Base.hexdigest(key, text) begin hmac = self.new(key) hmac.update(text) hmac.hexdigest ensure hmac.reset_key end end
new(algorithm, block_size, output_length, key)
click to toggle source
# File lib/hmac/hmac.rb, line 16 def initialize(algorithm, block_size, output_length, key) @algorithm = algorithm @block_size = block_size @output_length = output_length @status = STATUS_UNDEFINED @key_xor_ipad = '' @key_xor_opad = '' set_key(key) unless key.nil? end
Public Instance Methods
digest()
click to toggle source
# File lib/hmac/hmac.rb, line 74 def digest check_status @md.digest end
hexdigest()
click to toggle source
# File lib/hmac/hmac.rb, line 79 def hexdigest check_status @md.hexdigest end
Also aliased as: to_s
reset_key()
click to toggle source
# File lib/hmac/hmac.rb, line 51 def reset_key @key_xor_ipad.gsub!(/./, '?') @key_xor_opad.gsub!(/./, '?') @key_xor_ipad[0..-1] = '' @key_xor_opad[0..-1] = '' @status = STATUS_UNDEFINED end
set_key(key)
click to toggle source
# File lib/hmac/hmac.rb, line 35 def set_key(key) # If key is longer than the block size, apply hash function # to key and use the result as a real key. key = @algorithm.digest(key) if key.size > @block_size key_xor_ipad = "\x36" * @block_size key_xor_opad = "\x5C" * @block_size for i in 0 .. key.size - 1 key_xor_ipad[i] ^= key[i] key_xor_opad[i] ^= key[i] end @key_xor_ipad = key_xor_ipad @key_xor_opad = key_xor_opad @md = @algorithm.new @status = STATUS_INITIALIZED end
update(text)
click to toggle source
# File lib/hmac/hmac.rb, line 59 def update(text) check_status # perform inner H md = @algorithm.new md.update(@key_xor_ipad) md.update(text) str = md.digest # perform outer H md = @algorithm.new md.update(@key_xor_opad) md.update(str) @md = md end
Also aliased as: <<
Private Instance Methods
check_status()
click to toggle source
# File lib/hmac/hmac.rb, line 27 def check_status unless @status == STATUS_INITIALIZED raise RuntimeError, "The underlying hash algorithm has not yet been initialized." end end