Sample code for 30+ languages & platforms
React Native Requires Chilkat v10.1.2+

RSA Sign using Private Key of Certificate Type A3 (smart card / token)

See more RSA Examples

Demonstrates RSA signing data using the private key of a certificate type A3 (smart card, token).

Note: This is a Windows-only example.

Chilkat React Native Downloads

React Native
import { Cert, CertStore, FileAccess, JsonObject, Rsa } from '@chilkat/react-native'

function chilkatExample() {
  // First get the A3 certificate that was installed on the Windows system.
  const certStore = new CertStore();

  const thumbprint = '12c1dd8015f3f03f7b1fa619dc24e2493ca8b4b2';

  // This is specific to Windows because it is opening the Windows Current-User certificate store.
  const bReadOnly = true;
  try {
    certStore.openCurrentUserStore(bReadOnly);
  } catch {
    console.log(certStore.lastErrorText);
    return;
  }

  // Find the certificate with the desired thumbprint
  // (There are many ways to locate a certificate.  This example chooses to find by thumbprint.)
  const json = new JsonObject();
  json.updateString('thumbprint', thumbprint);

  const cert = new Cert();
  try {
    certStore.findCert(json, cert);
  } catch {
    console.log('Failed to find the certificate.');
    return;
  }

  console.log(`Found: ${cert.subjectCN}`);

  const rsa = new Rsa();

  // Provide the cert's private key
  const bUsePrivateKey = true;
  try {
    rsa.setX509Cert(cert, bUsePrivateKey);
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  // Now we're ready to sign..
  const fac = new FileAccess();

  // Get bytes to be signed..
  const inData = fac.readEntireFile('in.dat');
  const signature = rsa.signBytes(inData, 'SHA-256');
  if (!rsa.lastMethodSuccess) {
    console.log(rsa.lastErrorText);
    return;
  }

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