Class: Paymob::Hmac

Inherits:
Object
  • Object
show all
Defined in:
lib/paymob/hmac.rb

Class Method Summary collapse

Class Method Details

.calculate(hash) ⇒ Object



5
6
7
8
# File 'lib/paymob/hmac.rb', line 5

def self.calculate(hash)
  concatenated_params = flatten(hash.to_h).sort.to_h.values.join
  OpenSSL::HMAC.hexdigest('SHA512', hmac_key, concatenated_params)
end

.flatten(hash) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/paymob/hmac.rb', line 14

def self.flatten(hash)
  new_hash = {}
  hash.each do |key, value|
    if value.is_a?(Hash)
      new_hash.merge!(value.transform_keys { |k| "#{key}.#{k}" })
    else
      new_hash[key] = value
    end
  end
  new_hash
end

.hmac_keyObject



26
27
28
# File 'lib/paymob/hmac.rb', line 26

def self.hmac_key
  Paymob.hmac_secret
end

.matches_original?(hash, original_hmac) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/paymob/hmac.rb', line 10

def self.matches_original?(hash, original_hmac)
  calculate(hash) == original_hmac
end