RSA Signature

How to Generate an RSA Signature

How to Generate an RSA Signature

This guide explains how to generate a secure digital signature using an RSA private key and the SHA-256 hashing algorithm. A digital signature ensures that the data you're sending has not been tampered with and can be verified using the corresponding public key. This process is commonly used in authentication, secure communication, and digital transactions.

Prerequisites

  • Your private key must be in PEM format
 
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqh...
-----END PRIVATE KEY-----

Setup

 const crypto = require("crypto");
const fs = require("fs");
 
const privateKey = fs.readFileSync("private.key", "utf8");
const data = "your-data-string";
 
const sign = crypto.createSign("RSA-SHA256");
sign.update(data);
sign.end();
 
const signature = sign.sign(privateKey, "base64");

"your-data-string"?

Example:

  `amount=100` +
  `&api_secret=WwTQrX1SB3JvLwtlReFuL8mcnlpXI6SEN9YRgQRdNXb` +
  `&currency=ETB` +
  `&customerEmail=test12@gmail.com` +
  `&customerName=name` +
  `&customerPhoneNumber=+25198765...` +
  `&description=VANS+` +
  `&redirectUrl=https://google.com` +
  `&expiredAt=2025-07-01T23:59:59Z` +
  `callbackURL:https://example.com/start_pay_callback`
  `&items=[
    {
      "productId": "6812220726f547936d6c1976",
      "quantity": 4,
      "item_name": "mobile",
      "unit_price": 1209.5
    },
    {
      "productId": "6812220726f547936d6c1976",
      "quantity": 3,
      "item_name": "laptop",
      "unit_price": 18.5
    }
  ]`
 
 

When you use like the code above to generate a signature, you will receive a response containing a secure, encoded signature. This signature is created using your request data and private key, and it helps verify that the request is authentic and has not been modified. It's commonly used to protect payment or API requests from tampering or unauthorized access.

Example output:

signature= "RVepgh4hiwaF6jVRyAAcIyVM3r80BKb1jQNKIJAumQF97x17TtbfK8fWgcdd8tB3uXJhflgXoUV601F/oLh0GIOZ7+NHN4Km9IthCLal8XVgLnMvL9dZr/08vUPQ3/owlMMQS34wpAuImFacBfGiy43En3fPcDgA2/joGwJLW/74UPmRnFbVwCRQCHvsb0HeCoGmrRRlcHehfb2sAL6w7P3stN24+GeIrywyNMOV25mpi66rmSVvY+CkRnNPmpl6aLXgsWTITCI08fHNn/3xC40s9LzR0L8DRrLMvnhiZG9OS9xLsoseKH3aqk/I5h95yW4uH1NTG4F2Jiz6FmUxGA=="

On this page