Skip to content
class method

OpenSSL::HMAC.new

(::String key, ::String | ::OpenSSL::Digest digest) -> ::OpenSSL::HMAC
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'
instance = OpenSSL::HMAC.new(key, 'SHA1')
#=> 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', 'SHA1')
#=> f42bb0eeb018ebbd4597ae7213711ec60760843f
instance == other_instance
#=> true
See also .hexdigest #<< .digest #== also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0