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

Verify the RSA Signature of a SHA256 Hash

See more RSA Examples

Demonstrates how to verify an RSA signature of a SHA256 hash.

Chilkat React Native Downloads

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

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

  // Let's say you have a file containing the 32-bytes of a SHA256 hash,
  // and a file that is an RSA signature of those 32 bytes.
  // Here's how you verify using the RSA public key found in a PEM.

  const pubKey = new PublicKey();
  try {
    pubKey.loadFromFile('rsaPubKey.pem');
  } catch {
    console.log(pubKey.lastErrorText);
    return;
  }

  const rsa = new Rsa();

  // Get the public key.
  try {
    rsa.usePublicKey(pubKey);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  // Get the 32-byte SHA256 hash.
  const bdHash = new BinData();
  try {
    bdHash.loadFile('myHash.sha256');
  } catch {
    console.log('Failed to load SHA256 hash.');
    return;
  }

  // Get the RSA signature to be validated.
  const bdSig = new BinData();
  try {
    bdSig.loadFile('mySig.sig');
  } catch {
    console.log('Failed to load RSA signature.');
    return;
  }

  // Verify the signature against the SHA256 hash.
  const enc = 'base64';
  rsa.encodingMode = enc;
  try {
    rsa.verifyHashENC(bdHash.getEncoded(enc), 'sha256', bdSig.getEncoded(enc));
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  console.log('Signature validated.');
}