Sha256: ed6503df1c647954f602254d6b35c00468e3e54088a4ce48b97f672c78d25267

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require 'digest'

module Buckaruby
  # Calculate a signature based on the parameters of the payment request or response.
  # -> see BPE 3.0 Gateway NVP, chapter 4 'Digital Signature'
  class Signature
    def self.generate_signature(params, options)
      secret = options[:secret]
      hash_method = options[:hash_method]

      case hash_method
      when :sha1
        Digest::SHA1.hexdigest(generate_signature_string(params, secret))
      when :sha256
        Digest::SHA256.hexdigest(generate_signature_string(params, secret))
      when :sha512
        Digest::SHA512.hexdigest(generate_signature_string(params, secret))
      else
        raise ArgumentError, "Invalid hash method provided: #{hash_method}"
      end
    end

    def self.generate_signature_string(params, secret)
      sign_params = params.select { |key, _value| key.to_s.upcase.start_with?("BRQ_", "ADD_", "CUST_") && key.to_s.casecmp("BRQ_SIGNATURE").nonzero? }
      string = sign_params.sort_by { |p| p.first.downcase }.map { |param| "#{param[0]}=#{param[1]}" }.join
      string << secret
      string
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
buckaruby-1.0.2 lib/buckaruby/signature.rb
buckaruby-1.0.1 lib/buckaruby/signature.rb