Class: Authmac::HmacChecker
- Inherits:
-
Object
- Object
- Authmac::HmacChecker
- Defined in:
- lib/authmac/hmac_checker.rb
Instance Method Summary collapse
-
#initialize(secret, message_format: :values, parameter_separator: '|', digest_function: 'sha1') ⇒ HmacChecker
constructor
A new instance of HmacChecker.
- #sign(hash) ⇒ Object
- #validate(hash, given_hmac) ⇒ Object
- #with_signature(hash, hmac_key = :hmac) ⇒ Object
Constructor Details
#initialize(secret, message_format: :values, parameter_separator: '|', digest_function: 'sha1') ⇒ HmacChecker
Returns a new instance of HmacChecker.
8 9 10 11 12 13 14 15 16 |
# File 'lib/authmac/hmac_checker.rb', line 8 def initialize(secret, message_format: :values, parameter_separator: '|', digest_function: 'sha1') @secret = secret @digest = digest_function @separator = parameter_separator @message_format = fail Authmac::SecretError, 'secret too short, see rfc2104' unless @secret.bytes.size >= digester.digest_length * 2 end |
Instance Method Details
#sign(hash) ⇒ Object
22 23 24 |
# File 'lib/authmac/hmac_checker.rb', line 22 def sign(hash) OpenSSL::HMAC.hexdigest(digester, @secret, (hash)) end |
#validate(hash, given_hmac) ⇒ Object
18 19 20 |
# File 'lib/authmac/hmac_checker.rb', line 18 def validate(hash, given_hmac) sign(hash) == given_hmac end |
#with_signature(hash, hmac_key = :hmac) ⇒ Object
26 27 28 |
# File 'lib/authmac/hmac_checker.rb', line 26 def with_signature(hash, hmac_key = :hmac) hash.merge({hmac_key => sign(hash)}) end |