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

RSA Hash Binary Data and Sign (and Verify)

See more RSA Examples

Demonstrates how to sign the hash of binary data. Also demonstrates how to verify the RSA signature.

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 bdData = new BinData();
  bdData.appendEncoded(originalData, 'hex');

  // Hash (SHA-256) and sign the hash:
  const bdSignature = new BinData();
  try {
    rsa.signBd(bdData, 'sha256', bdSignature);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

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

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

  // Verify the signature..
  const rsa2 = new Rsa();
  rsa2.usePublicKey(pubKey);

  let bVerified = true;
  try {
    rsa2.verifyBd(bdData, 'sha256', bdSignature);
  } catch {
    bVerified = false;
  }
  console.log(`signature verified: ${bVerified}`);
}