React Native Requires Chilkat v10.1.2+
React Native
Create PKCS7 Signed File (.p7m)
See more Encryption Examples
Demonstrates how to sign a file to create a .p7m that contains both the file contents and the signature.Chilkat React Native Downloads
import { Cert, CertStore, Crypt2, JsonObject } 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 crypt = new Crypt2();
const certStore = new CertStore();
// Load a PFX file into a certificate store object.
try {
certStore.loadPfxFile('myPfx.pfx', 'pfxPassword');
} catch {
console.log(certStore.lastErrorText);
return;
}
// Get the certificate by subject common name.
// This should be the cert within the PFX that also
// has a private key (also stored within the PFX).
const jsonCN = new JsonObject();
jsonCN.updateString('CN', 'myCert');
const cert = new Cert();
try {
certStore.findCert(jsonCN, cert);
} catch {
console.log(certStore.lastErrorText);
return;
}
// Tell the crypt object to use the certificate for signing:
crypt.setSigningCert(cert);
// Sign a file, producing a .p7m as output.
// The input file is unchanged, the test.p7m contains the
// contents of the input file and the signature.
const inFile = 'test.txt';
const outFile = 'testp7m';
try {
await crypt.createP7MAsync(inFile, outFile);
} catch {
console.log(crypt.lastErrorText);
return;
}
console.log('Success!');
}