React Native
React Native
Sign a Byte Array to Create a Detached Signature in a Byte Array
See more Digital Signatures Examples
Signs data contained in a byte array to produce a detached signature (also in a byte array). Also shows how to verify the signature.Chilkat React Native Downloads
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.
// The result is a detached signature (a signature that does not contain the data being signed).
const sigBytes = await crypt.signBytesAsync(fileBytes);
if (!crypt.lastMethodSuccess) {
console.log(crypt.lastErrorText);
return;
}
try {
fac.writeEntireFile('qa_output/sample.pdf.p7s', sigBytes);
} catch {
console.log(fac.lastErrorText);
return;
}
// We can verify the detached signature like this
let verified = true;
try {
crypt.verifyBytes(fileBytes, sigBytes);
} catch {
verified = false;
}
if (!verified) {
console.log(crypt.lastErrorText);
return;
}
console.log(`Verified = ${verified}`);
}