Sha256: 40b50cb5a4839591060357b0c86a21a2f855acf738d4d2c385746d9cbd79cbe3

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'openssl'

module Razorpay
  # Helper functions are defined here
  class Utility
    def self.verify_payment_signature(attributes)
      signature = attributes[:razorpay_signature]
      order_id = attributes[:razorpay_order_id]
      payment_id = attributes[:razorpay_payment_id]

      data = [order_id, payment_id].join '|'

      secret = Razorpay.auth[:password]

      verify_signature(data, signature, secret)
    end

    def self.verify_webhook_signature(body, signature, secret)
      verify_signature(body, signature, secret)
    end

    class << self
      private

      def verify_signature(data, signature, secret)
        expected_signature = OpenSSL::HMAC.hexdigest('SHA256', secret, data)

        verified = secure_compare(expected_signature, signature)

        raise SecurityError, 'Signature verification failed' unless verified
      end

      def secure_compare(a, b)
        return false unless a.bytesize == b.bytesize

        l = a.unpack('C*')
        r = 0
        i = -1

        b.each_byte do |v|
          i += 1
          r |= v ^ l[i]
        end

        r.zero?
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
razorpay-2.2.0 lib/razorpay/utility.rb
razorpay-2.1.0 lib/razorpay/utility.rb
razorpay-2.1.0.pre lib/razorpay/utility.rb
razorpay-2.0.1 lib/razorpay/utility.rb