Class: Prodamus::Verifier
- Inherits:
-
Object
- Object
- Prodamus::Verifier
- Defined in:
- lib/services/verifier.rb
Overview
HMAC encoding and verify signature
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#data ⇒ Object
Returns the value of attribute data.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #encode ⇒ Object
-
#initialize(data, key, algorithm = 'sha256') ⇒ Verifier
constructor
A new instance of Verifier.
- #verify(sign) ⇒ Object
Constructor Details
#initialize(data, key, algorithm = 'sha256') ⇒ Verifier
10 11 12 13 14 |
# File 'lib/services/verifier.rb', line 10 def initialize(data, key, algorithm = 'sha256') @data = data @key = key @algorithm = algorithm end |
Instance Attribute Details
#algorithm ⇒ Object
Returns the value of attribute algorithm.
8 9 10 |
# File 'lib/services/verifier.rb', line 8 def algorithm @algorithm end |
#data ⇒ Object
Returns the value of attribute data.
8 9 10 |
# File 'lib/services/verifier.rb', line 8 def data @data end |
#key ⇒ Object
Returns the value of attribute key.
8 9 10 |
# File 'lib/services/verifier.rb', line 8 def key @key end |
Instance Method Details
#encode ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/services/verifier.rb', line 21 def encode data = sort(@data).to_json digest = OpenSSL::Digest.new(@algorithm) OpenSSL::HMAC.hexdigest(digest, @key, data) rescue NoMethodError raise ArgumentError, 'Expected a Hash with array of hashes.' end |
#verify(sign) ⇒ Object
16 17 18 19 |
# File 'lib/services/verifier.rb', line 16 def verify(sign) encoded_data = encode encoded_data && (encoded_data == sign) end |