Class: Prodamus::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/services/verifier.rb

Overview

HMAC encoding and verify signature

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#algorithmObject

Returns the value of attribute algorithm.



8
9
10
# File 'lib/services/verifier.rb', line 8

def algorithm
  @algorithm
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/services/verifier.rb', line 8

def data
  @data
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/services/verifier.rb', line 8

def key
  @key
end

Instance Method Details

#encodeObject



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