Generate Signature
Authenticating your request
Overview
signature is an encrypted string that acts as a crucial part of the authentication process. It ensures and confirms the information included in the request is genuine and hasn't been modified. The user has to first generate a Signature Code and pass it through the request based on which the system detects and authenticates the request and provides a response. Any request with an invalid or unmatched Signature will be responded to as "Invalid/Unauthorized".
Generate signature
Here's a sample to generate a signature:
const crypto = require("crypto");
const SIGNATURE_KEY = "1234567890"; // your shared key
function generateSignature(reqBody) {
const jsonString = JSON.stringify(reqBody);
const base64Data = Buffer.from(jsonString, "utf-8").toString("base64");
const dataToHash = base64Data + SIGNATURE_KEY;
const signature = crypto.createHash("sha256").update(dataToHash).digest("hex");
return signature;
}
const body = {
clientId: "abcd",
clientCode: "xyz",
merchantId: "merchant",
grantType: "client_credentials"
};
console.log("Generated Signature:", generateSignature(body));import hashlib, base64, json
SIGNATURE_KEY = "1234567890"
def generate_signature(req_body):
# Step 1: Convert to compact JSON string (no spaces)
json_str = json.dumps(req_body, separators=(',', ':'))
# Step 2: Base64 encode
base64_data = base64.b64encode(json_str.encode('utf-8')).decode('utf-8')
# Step 3: Append signature key
data_to_hash = base64_data + SIGNATURE_KEY
# Step 4: SHA256 hash in hex
signature = hashlib.sha256(data_to_hash.encode('utf-8')).hexdigest()
return signature
body = {
"clientId": "abcd",
"clientCode": "xyz",
"merchantId": "merchant",
"grantType": "client_credentials"
}
print("Generated Signature:", generate_signature(body))import java.security.MessageDigest;
import java.util.Base64;
import com.google.gson.Gson;
import java.util.*;
public class SignatureGenerator {
private static final String SIGNATURE_KEY = "1234567890";
public static String generateSignature(Map<String, Object> reqBody) throws Exception {
// Step 1: Convert to JSON string
String json = new Gson().toJson(reqBody);
// Step 2: Base64 encode
String base64Data = Base64.getEncoder().encodeToString(json.getBytes("UTF-8"));
// Step 3: Append signature key
String dataToHash = base64Data + SIGNATURE_KEY;
// Step 4: SHA256 hash
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(dataToHash.getBytes("UTF-8"));
// Convert to hex
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
}
public static void main(String[] args) throws Exception {
Map<String, Object> body = new LinkedHashMap<>();
body.put("clientId", "abcd");
body.put("clientCode", "xyz");
body.put("merchantId", "merchant");
body.put("grantType", "client_credentials");
System.out.println("Generated Signature: " + generateSignature(body));
}
}using System;
using System.Text;
using System.Security.Cryptography;
using Newtonsoft.Json;
using System.Collections.Generic;
class Program
{
const string SIGNATURE_KEY = "1234567890";
static string GenerateSignature(object reqBody)
{
// Step 1: Convert to JSON string
var json = JsonConvert.SerializeObject(reqBody);
// Step 2: Base64 encode
var base64Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));
// Step 3: Append signature key
var dataToHash = base64Data + SIGNATURE_KEY;
// Step 4: SHA256 hash
using (var sha256 = SHA256.Create())
{
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(dataToHash));
var sb = new StringBuilder();
foreach (var b in hash)
sb.Append(b.ToString("x2"));
return sb.ToString();
}
}
static void Main()
{
var body = new
{
clientId = "abcd",
clientCode = "xyz",
merchantId = "merchant",
grantType = "client_credentials"
};
Console.WriteLine("Generated Signature: " + GenerateSignature(body));
}
}<?php
function generateSignature($reqBody) {
$SIGNATURE_KEY = "1234567890";
// Step 1: Convert to JSON string
$jsonString = json_encode($reqBody, JSON_UNESCAPED_SLASHES);
// Step 2: Base64 encode
$base64Data = base64_encode($jsonString);
// Step 3: Append SIGNATURE_KEY
$dataToHash = $base64Data . $SIGNATURE_KEY;
// Step 4: Generate SHA256 hash (hex)
$signature = hash("sha256", $dataToHash);
return $signature;
}
$body = [
"clientId" => "abcd",
"clientCode" => "xyz",
"merchantId" => "merchant",
"grantType" => "client_credentials"
];
echo "Generated Signature: " . generateSignature($body);
?>package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
)
const SIGNATURE_KEY = "1234567890"
func generateSignature(reqBody interface{}) string {
// Step 1: Convert to JSON string
jsonBytes, _ := json.Marshal(reqBody)
// Step 2: Base64 encode
base64Data := base64.StdEncoding.EncodeToString(jsonBytes)
// Step 3: Append SIGNATURE_KEY
dataToHash := base64Data + SIGNATURE_KEY
// Step 4: Generate SHA256 hash (hex)
hash := sha256.Sum256([]byte(dataToHash))
return hex.EncodeToString(hash[:])
}
func main() {
body := map[string]string{
"clientId": "abcd",
"clientCode": "xyz",
"merchantId": "merchant",
"grantType": "client_credentials",
}
fmt.Println("Generated Signature:", generateSignature(body))
}
require 'json'
require 'base64'
require 'digest'
SIGNATURE_KEY = "1234567890"
def generate_signature(req_body)
# Step 1: Convert to JSON string
json_str = req_body.to_json
# Step 2: Base64 encode
base64_data = Base64.strict_encode64(json_str)
# Step 3: Append signature key
data_to_hash = base64_data + SIGNATURE_KEY
# Step 4: Generate SHA256 hash (hex)
signature = Digest::SHA256.hexdigest(data_to_hash)
signature
end
body = {
clientId: "abcd",
clientCode: "xyz",
merchantId: "merchant",
grantType: "client_credentials"
}
puts "Generated Signature: #{generate_signature(body)}"
Note
signaturevaries for every request and no two requests will have the same signature- ❌ In case of Signature Key mismatches, an "Unauthorized" or "Invalid" response will be reflected
- ⚠️ If the process encounters a technical error, the system will respond to the request as "Technical/Internal/Server Error"
Updated 17 days ago