Class: Paymob::Hmac
- Inherits:
-
Object
- Object
- Paymob::Hmac
- Defined in:
- lib/paymob/hmac.rb
Class Method Summary collapse
- .calculate(hash) ⇒ Object
- .flatten(hash) ⇒ Object
- .hmac_key ⇒ Object
- .matches_original?(hash, original_hmac) ⇒ Boolean
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_key ⇒ Object
26 27 28 |
# File 'lib/paymob/hmac.rb', line 26 def self.hmac_key Paymob.hmac_secret end |
.matches_original?(hash, original_hmac) ⇒ Boolean
10 11 12 |
# File 'lib/paymob/hmac.rb', line 10 def self.matches_original?(hash, original_hmac) calculate(hash) == original_hmac end |