Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

RSA Sign Binary Data and Verify (Recover the Original Data)

See more RSA Examples

Demonstrates how to RSA sign binary data and then verify/recover the original data.

Note: This example uses methods introduced in Chilkat v9.5.0.77.

Chilkat React Native Downloads

React Native
import { BinData, PrivateKey, PublicKey, Rsa } from '@chilkat/react-native'

function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // Load an RSA private key for signing.
  const privKey = new PrivateKey();
  try {
    privKey.loadEncryptedPemFile('qa_data/pem/rsa_passwd.pem', 'passwd');
  } catch {
    console.log(privKey.lastErrorText);
    return;
  }

  const rsa = new Rsa();
  rsa.usePrivateKey(privKey);

  // We have some binary data (in hex) to sign
  const originalData = '0102030405060708090A';
  const bd = new BinData();
  bd.appendEncoded(originalData, 'hex');

  // If successful, the contents of bd are replaced with the RSA signature.
  try {
    rsa.signRawBd(bd);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  // Show the RSA signature in base64
  console.log(bd.getEncoded('base64'));

  // ------------------------------------------
  // Get the public key from the private key
  const pubKey = new PublicKey();
  privKey.toPublicKey(pubKey);

  // Verify the signature and extract the original data.
  const rsa2 = new Rsa();
  rsa2.usePublicKey(pubKey);

  let bVerified = true;
  try {
    rsa2.verifyRawBd(bd);
  } catch {
    bVerified = false;
  }
  console.log(`signature verified: ${bVerified}`);

  // Show the original data:
  console.log(`original data: ${bd.getEncoded('hex')}`);
}