Class: CryptoToolchain::Utilities::HMAC
- Defined in:
- lib/crypto_toolchain/utilities/hmac.rb
Class Method Summary collapse
- .digest(message, key:, hash: CryptoToolchain::Utilities::SHA1) ⇒ Object
- .hexdigest(message, key:, hash: CryptoToolchain::Utilities::SHA1) ⇒ Object
Instance Method Summary collapse
- #determine_blocksize ⇒ Object
- #digest(message) ⇒ Object
- #hexdigest(message) ⇒ Object
-
#initialize(key:, hash:, blocksize: nil) ⇒ HMAC
constructor
A new instance of HMAC.
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(, key: , hash: CryptoToolchain::Utilities::SHA1) new(key: key, hash: hash).digest() end |
.hexdigest(message, key:, hash: CryptoToolchain::Utilities::SHA1) ⇒ Object
9 10 11 |
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 9 def hexdigest(, key: , hash: CryptoToolchain::Utilities::SHA1) new(key: key, hash: hash).hexdigest() end |
Instance Method Details
#determine_blocksize ⇒ Object
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() hash.digest(outer_pad + hash.digest(inner_pad + )) end |
#hexdigest(message) ⇒ Object
25 26 27 |
# File 'lib/crypto_toolchain/utilities/hmac.rb', line 25 def hexdigest() digest().to_hex end |