Skip to content

Commit

Permalink
code for generating onboarding signature
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavyay committed Feb 22, 2024
1 parent d330cb8 commit 5cb3c10
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/razorpay/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def self.verify_webhook_signature(body, signature, secret)
verify_signature(body, signature, secret)
end

def self.generate_onboarding_signature(body, secret)
json_data = body.to_json
encrypt(json_data, secret);
end

class << self
private

Expand All @@ -52,6 +57,25 @@ def secure_compare(a, b)

r.zero?
end

def encrypt(data, secret)
iv = secret[0, 12]
key = secret[0, 16]

cipher = OpenSSL::Cipher.new('aes-128-gcm')
cipher.encrypt
cipher.key = key
cipher.iv = iv

cipher.auth_data = ""

encrypted = cipher.update(data) + cipher.final

tag = cipher.auth_tag
combined_encrypted_data = encrypted + tag

encrypted_data_hex = combined_encrypted_data.unpack1("H*")
end
end
end
end
34 changes: 34 additions & 0 deletions test/razorpay/test_utility.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'json'

module Razorpay
# Tests for Razorpay::Utility
Expand Down Expand Up @@ -80,5 +81,38 @@ def test_webhook_signature_verification
Razorpay::Utility.verify_webhook_signature(webhook_body, signature, secret)
end
end

def test_generate_onboarding_signature
secret = "EnLs21M47BllR3X8PSFtjtbd"
timestamp = Time.now.to_i
body = {
submerchant_id: 'NSgKfYIR2f9v2y',
timestamp: timestamp
}
encryptedData = Razorpay::Utility.generate_onboarding_signature(body, secret)
json_data = decrypt(encryptedData, secret)
body = JSON.parse(json_data)
assert_equal 'NSgKfYIR2f9v2y', body['submerchant_id'], 'Submerchant IDs do not match'
assert_equal timestamp, body['timestamp'], 'Timestamps do not match'
end

def decrypt(data, secret)
combined_encrypted_data = [data].pack("H*")

iv = secret[0, 12]
key = secret[0, 16]
tag = combined_encrypted_data[-16..]

encrypted_data = combined_encrypted_data[0...-16]

cipher = OpenSSL::Cipher.new('aes-128-gcm')
cipher.decrypt
cipher.key = key
cipher.iv = iv
cipher.auth_tag = tag
cipher.auth_data = ""

cipher.update(encrypted_data) + cipher.final
end
end
end

0 comments on commit 5cb3c10

Please sign in to comment.