Sha256: 124f907c5f50fb8bde6f2c392e8e6e10fe66293c583763e8f08af67fd410c67e

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

require 'openssl'

module Razorpay
  # Helper functions are defined here
  class Utility
    def self.verify_payment_signature(attributes)
      signature = attributes.delete(:razorpay_signature) 

      # Data requires the values to be in sorted order of their keys.
      # attributes.sort returns a nested array, and the last
      # element of each is the value. These are joined.
      data = attributes.sort.map(&:last).join('|')
      secret = Razorpay.auth[:password]
      verify_signature(data, signature, secret)
    end

    def self.verify_payment_link_signature(attributes)
      signature = attributes.delete(:razorpay_signature)
      # element of each is the value. These are joined.
      data = attributes.values.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

        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

2 entries across 2 versions & 1 rubygems

Version Path
razorruby-3.0.4 lib/razorpay/utility.rb
razorruby-3.0.1 lib/razorpay/utility.rb