Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final cert = CkCert();
try {
cert.loadPfxFile('qa_data/pfx/cert_test123.pfx', 'test123');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final fac = CkFileAccess();
final fileBytes = fac.readEntireFile('qa_data/pdf/sample.pdf');
if (!fac.lastMethodSuccess) {
print(fac.lastErrorText);
return;
}
final crypt = CkCrypt2();
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).
final sigBytes = crypt.signBytes(fileBytes);
if (!crypt.lastMethodSuccess) {
print(crypt.lastErrorText);
return;
}
try {
fac.writeEntireFile('qa_output/sample.pdf.p7s', sigBytes);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// We can verify the detached signature like this
var verified = true;
try {
crypt.verifyBytes(fileBytes, sigBytes);
} on ChilkatException {
verified = false;
}
if (!verified) {
print(crypt.lastErrorText);
return;
}
print('Verified = $verified');
}