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

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

See more RSA Examples

Demonstrates RSA signing a string 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, 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;
  }

  // Return the RSA signature in base64 encoded form.
  rsa.encodingMode = 'base64';

  // Sign the utf-8 byte representation of the string.
  rsa.charset = 'utf-8';

  // You can also choose other hash algorithms, such as SHA-1.
  let sigBase64: string;
  try {
    sigBase64 = rsa.signStringENC('text to sign', 'SHA-256');
  } catch {
    console.log(rsa.lastErrorText);
    return;
  }

  console.log(`Base64 signature: ${sigBase64}`);
}