Module: Adyen::Signature

Extended by:
Signature
Included in:
Signature
Defined in:
lib/adyen/signature.rb

Overview

The Signature module generic to sign and verify HMAC SHA-256 signatures

Instance Method Summary collapse

Instance Method Details

#sign(params, type = :hpp) ⇒ String

Sign the parameters with the given shared secret

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/adyen/signature.rb', line 14

def sign(params, type = :hpp)
  shared_secret = params.delete('sharedSecret')
  raise ArgumentError, "Cannot sign parameters without a shared secret" unless shared_secret
  sig = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), Array(shared_secret).pack("H*"), string_to_sign(params, type))
  Base64.encode64(sig).strip
end

#verify(params, hmacSignature, type = :hpp) ⇒ Boolean

Compare a signature calculated with anoter HMAC Signature

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/adyen/signature.rb', line 26

def verify(params, hmacSignature, type = :hpp)
  raise ArgumentError,"hmacSignature cannot be empty for verification" if hmacSignature.empty?
  our_sig = sign(params, type)
  secure_compare(hmacSignature, our_sig)
end