Sample code for 30+ languages & platforms
React Native

Sign a Byte Array to Create an Opaque Signature in a Byte Array

See more Digital Signatures Examples

Signs data contained in a byte array to produce an opaque signature (also in a byte array). An opaque signature is a PKCS7 signature (also known as CAdES) that embeds the signed data. Also shows how to verify the signature and extract the original data.

Chilkat React Native Downloads

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

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

  const cert = new Cert();
  try {
    cert.loadPfxFile('qa_data/pfx/cert_test123.pfx', 'test123');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  const fac = new FileAccess();

  const fileBytes = fac.readEntireFile('qa_data/pdf/sample.pdf');
  if (!fac.lastMethodSuccess) {
    console.log(fac.lastErrorText);
    return;
  }

  const crypt = new Crypt2();

  crypt.setSigningCert(cert);

  // We can sign any type of file.

  const sigBytes = await crypt.opaqueSignBytesAsync(fileBytes);
  if (!crypt.lastMethodSuccess) {
    console.log(crypt.lastErrorText);
    return;
  }

  try {
    fac.writeEntireFile('qa_output/sample.pdf.p7m', sigBytes);
  } catch {
    console.log(fac.lastErrorText);
    return;
  }

  // We can verify the opaque signature and extract the original data like this
  const originalData = crypt.opaqueVerifyBytes(sigBytes);
  if (!crypt.lastMethodSuccess) {
    console.log(crypt.lastErrorText);
    return;
  }

  try {
    fac.writeEntireFile('qa_output/sample.pdf', originalData);
  } catch {
    console.log(fac.lastErrorText);
    return;
  }

  console.log('Signature is verified and the original data was extracted.');
}