Module: Adyen::HPP::Signature

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

Overview

The Signature module can sign and verify HMAC SHA-256 signatures for Hosted Payment Pages

Instance Method Summary collapse

Instance Method Details

#sign(params, shared_secret = nil) ⇒ Hash

Sign the parameters with the given shared secret

Parameters:

  • params (Hash)

    The set of parameters to sign

  • shared_secret (String) (defaults to: nil)

    The shared secret for signing/verification. Can also be sent in the params hash with the ‘sharedSecret` key.

Returns:

  • (Hash)

    params The params that were passed in plus a new ‘merchantSig` param

Raises:

  • (ArgumentError)


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

def sign(params, shared_secret = nil)
  shared_secret ||= params.delete['sharedSecret']
  raise ArgumentError, "Cannot verify a signature without a shared secret" unless shared_secret
  sig = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), Array(shared_secret).pack("H*"), string_to_sign(params))
  params.merge('merchantSig' => Base64.encode64(sig).strip)
end

#verify(params, shared_secret = nil) ⇒ Boolean

Verify the parameters with the given shared secret

Parameters:

  • params (Hash)

    The set of parameters to verify. Must include a ‘merchantSig` param that will be compared to the signature we calculate.

  • shared_secret (String) (defaults to: nil)

    The shared secret for signing/verification. Can also be sent in the params hash with the ‘sharedSecret` key.

Returns:

  • (Boolean)

    true if the ‘merchantSig` in the params matches our calculated signature

Raises:

  • (ArgumentError)


28
29
30
31
32
33
# File 'lib/adyen/hpp/signature.rb', line 28

def verify(params, shared_secret = nil)
  their_sig = params.delete('merchantSig')
  raise ArgumentError, "params must include 'merchantSig' for verification" if their_sig.empty?
  our_sig = sign(params, shared_secret)['merchantSig']
  secure_compare(their_sig, our_sig)
end