Get Stated
Authentication ( Optional )
// Sample Code In PHP
// Step 1: Prepare the data
$msisdn = "256757121212";
$transactionID = "876767672323";
$amount = "5000";
$data = $msisdn."&".$transactionID."&".$amount;
// Step 2: Load your private key
$privateKey = openssl_pkey_get_private("path/to/private_key.pem");
// Make sure to replace 'path/to/private_key.pem' with the actual path to your private key file
// Step 3: Sign the data
openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256);
// Step 4: Encode the signature
$encodedSignature = base64_encode($signature);
// Print or use the JSON string as needed
echo $encodedSignature;// Sample Code In Java
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.util.Base64;
public class RSASigningExample {
public static void main(String[] args) throws Exception {
// Step 1: Prepare the data
String msisdn = "256757121212";
String transactionID = "876767672323";
String amount = "5000";
String data = msisdn + "&" + transactionID +"&" + amount;
// Step 2: Load your private key
byte[] privateKeyBytes = Files.readAllBytes(Paths.get("/path/to/private_key.pem"));
// Make sure to replace "/path/to/private_key.pem" with the actual path to your private key file
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
// Step 3: Sign the data
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(data.getBytes(StandardCharsets.UTF_8));
byte[] signedData = signature.sign();
// Step 4: Encode the signature
String encodedSignature = Base64.getEncoder().encodeToString(signedData);
// Print or use the JSON body as needed
System.out.println(encodedSignature);
}
}
Last updated