Class: Authmac::HmacChecker
- Inherits:
-
Object
- Object
- Authmac::HmacChecker
- Defined in:
- lib/authmac/hmac_checker.rb
Instance Method Summary collapse
-
#initialize(secret, 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, parameter_separator = '|', digest_function = 'sha1') ⇒ HmacChecker
Returns a new instance of HmacChecker.
5 6 7 8 9 10 |
# File 'lib/authmac/hmac_checker.rb', line 5 def initialize(secret, parameter_separator = '|', digest_function = 'sha1') @secret = secret @digest = digest_function @separator = parameter_separator fail Authmac::SecretError, 'secret too short, see rfc2104' unless @secret.bytes.size >= digester.digest_length * 2 end |
Instance Method Details
#sign(hash) ⇒ Object
16 17 18 |
# File 'lib/authmac/hmac_checker.rb', line 16 def sign(hash) OpenSSL::HMAC.hexdigest(digester, @secret, (hash)) end |
#validate(hash, given_hmac) ⇒ Object
12 13 14 |
# File 'lib/authmac/hmac_checker.rb', line 12 def validate(hash, given_hmac) sign(hash) == given_hmac end |
#with_signature(hash, hmac_key = :hmac) ⇒ Object
20 21 22 |
# File 'lib/authmac/hmac_checker.rb', line 20 def with_signature(hash, hmac_key = :hmac) hash.merge({hmac_key => sign(hash)}) end |