Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

OpenSSL::HMAC.new

HMAC.new(key, digest) → hmac

Returns an instance of OpenSSL::HMAC set with the key and digest algorithm to be used. The instance represents the initial state of the message authentication code before any data has been processed. To process data with it, use the instance method update with your data as an argument.

Example

Example
key = 'key'
digest = OpenSSL::Digest.new('sha1')
instance = OpenSSL::HMAC.new(key, digest)
#=> f42bb0eeb018ebbd4597ae7213711ec60760843f
instance.class
#=> OpenSSL::HMAC

A note about comparisons

Two instances can be securely compared with == in constant time:

Example
    other_instance = OpenSSL::HMAC.new('key', OpenSSL::Digest.new('sha1'))
#=> f42bb0eeb018ebbd4597ae7213711ec60760843f
instance == other_instance
#=> true
See also .hexdigest #<< .digest #== also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1