Class: CoderDecorator::Coders::HMAC
- Defined in:
- lib/coder_decorator/coders/hmac.rb
Overview
Actuall, it doesn’t encode, instead, it appends a hex HMAC to the input data in this format:
"#{data}--#{hmac}"
Constant Summary collapse
- REGEXP =
/\A(.*)--(.*)\z/
Constants inherited from Coder
Instance Method Summary collapse
- #decode(str) ⇒ Object
- #encode(str) ⇒ Object
-
#initialize(coder = nil, secret:, old_secret: nil, digest: 'SHA1') ⇒ HMAC
constructor
A new instance of HMAC.
Methods inherited from Coder
Constructor Details
#initialize(coder = nil, secret:, old_secret: nil, digest: 'SHA1') ⇒ HMAC
Returns a new instance of HMAC.
15 16 17 18 19 20 |
# File 'lib/coder_decorator/coders/hmac.rb', line 15 def initialize(coder = nil, secret:, old_secret: nil, digest: 'SHA1') super(coder) @secret = secret @old_secret = old_secret @digest = ::OpenSSL::Digest.new(digest) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class CoderDecorator::Coders::Coder
Instance Method Details
#decode(str) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/coder_decorator/coders/hmac.rb', line 28 def decode(str) match_data = REGEXP.match(str) data, hmac = match_data && match_data.captures secrets = [@secret, @old_secret] raise InvalidEncoding unless data && hmac && secrets.any? { |secret| secure_compare(hmac, generate_hmac(secret, data)) } coder.decode(data) end |
#encode(str) ⇒ Object
22 23 24 25 26 |
# File 'lib/coder_decorator/coders/hmac.rb', line 22 def encode(str) data = coder.encode(str) hmac = generate_hmac(@secret, data) "#{data}--#{hmac}" end |