Sample code for 30+ languages & platforms
Dart

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 Dart Downloads

Dart
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.

  final sigBytes = crypt.opaqueSignBytes(fileBytes);
  if (!crypt.lastMethodSuccess) {
    print(crypt.lastErrorText);
    return;
  }

  try {
    fac.writeEntireFile('qa_output/sample.pdf.p7m', sigBytes);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  try {
    fac.writeEntireFile('qa_output/sample.pdf', originalData);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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