Class: CryptoToolchain::Utilities::HMAC

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto_toolchain/utilities/hmac.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, hash:, blocksize: nil) ⇒ HMAC

Returns a new instance of HMAC.



15
16
17
18
19
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 15

def initialize(key: , hash: , blocksize: nil)
  @key = key
  @hash = hash
  @blocksize = blocksize || determine_blocksize
end

Class Method Details

.digest(message, key:, hash: CryptoToolchain::Utilities::SHA1) ⇒ Object



5
6
7
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 5

def digest(message, key: , hash: CryptoToolchain::Utilities::SHA1)
  new(key: key, hash: hash).digest(message)
end

.hexdigest(message, key:, hash: CryptoToolchain::Utilities::SHA1) ⇒ Object



9
10
11
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 9

def hexdigest(message, key: , hash: CryptoToolchain::Utilities::SHA1)
  new(key: key, hash: hash).hexdigest(message)
end

Instance Method Details

#determine_blocksizeObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 29

def determine_blocksize
  case hash.to_s.split(':').last.downcase.gsub(/[^a-z0-9]/i, '')
  when /md(4|5)/
    64
  when /sha(1|224|256)/
    64
  else
    raise ArgumentError.new("Unsupported hash #{hash}")
  end
end

#digest(message) ⇒ Object



21
22
23
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 21

def digest(message)
  hash.digest(outer_pad + hash.digest(inner_pad + message))
end

#hexdigest(message) ⇒ Object



25
26
27
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 25

def hexdigest(message)
  digest(message).to_hex
end