Skip to Content
NexPDF

Digital signatures

Sign PDFs with PAdES using a PKCS#12 file, add long-term validation and verify signatures from Flutter through the NexPDF C API.

English

License module

Signing, LTV and verification (with or without your own roots) need the signatures module (Business tier and above).

The signing and verification functions have no Dart method (document timestamps do; see Long-term signatures). Call them through dart:ffi. The native reader also lets the user draw a handwritten signature and sign with a certificate.

Every signing call returns the complete signed file. Write those bytes as they are: saving the document again afterwards breaks the signature.

Sign with a PKCS#12 file

import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';

// NexPdfBytes nexpdf_sign_pkcs12(const NexPdfDocument *doc, const uint8_t *p12, size_t p12_len,
//     const char *password, const char *options_json);
final signPkcs12 = nexpdfLib.lookupFunction<
    NexPdfBytes Function(Pointer<Void>, Pointer<Uint8>, Size, Pointer<Utf8>, Pointer<Utf8>),
    NexPdfBytes Function(Pointer<Void>, Pointer<Uint8>, int, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_sign_pkcs12');

final options = jsonEncode({
  'reason': 'Approved',
  'location': 'Lisbon',
  'tsa_url': 'http://timestamp.example.com',   // optional: PAdES B-T
  'ltv': true,                                   // optional: PAdES B-LT
  'appearance': {'page': 0, 'rect': [360, 60, 560, 120], 'text': 'Signed by Ana Silva'},
});

final signed = using((a) => nexpdfCall(() => takeBytes(signPkcs12(doc.handle, toNative(p12, a), p12.length,
    'p12-password'.toNativeUtf8(allocator: a), options.toNativeUtf8(allocator: a)))));
await File(outputPath).writeAsBytes(signed);

Options: name, reason, location, contact, field (an existing signature field), tsa_url, ltv, level (B-B, B-T, B-LT or B-LTA, see Long-term signatures), timestamp (the B-LTA document timestamp's hash, field and reserved) and appearance (page, rect, ink strokes, text). A wrong password throws NexPdfException('incorrect password').

Verify signatures

// char *nexpdf_verify_signatures_json(const uint8_t *data, size_t len);
final verify = nexpdfLib.lookupFunction<Pointer<Utf8> Function(Pointer<Uint8>, Size),
    Pointer<Utf8> Function(Pointer<Uint8>, int)>('nexpdf_verify_signatures_json');

final json = using((a) => nexpdfCall(() => takeString(verify(toNative(signed, a), signed.length))));
for (final s in (jsonDecode(json ?? '[]') as List).cast<Map<String, Object?>>()) {
  final ok = s['digest_ok'] == true && s['signature_ok'] == true;
  print('${s['signer']} ${s['level']} ${ok ? 'valid' : 'INVALID'}');
}

Each entry includes field, signer, subject, digest_ok, signature_ok, covers_whole_document, reason, signing_time, timestamp_ok, chain_ok, trusted, revocation, ltv, level, pades_level (B-B, B-T, B-LT or B-LTA) and document_timestamp (true for a document timestamp, whose signer is the timestamp authority).

Other signature functions

C functionDoes
nexpdf_add_ltv(data, len)Adds the OCSP responses and CRLs (a DSS) to a signed file
nexpdf_verify_signatures_with_roots_json(data, len, roots_pem)Verifies against your own root certificates
nexpdf_pkcs12_info_json(p12, len, password)Shows the certificate of a PKCS#12 file
nexpdf_prepare_external_signature, nexpdf_external_signature_data, nexpdf_complete_external_signatureSigning with an HSM, smart card or cloud key, in three calls with no state kept between them
nexpdf_sign_pades(doc, signer, reason, location)Signs with a new self-signed certificate, for tests
nexpdf_add_ink_signature(doc, page, color_hex, stroke_width, strokes)A drawn signature without cryptography

For external signing, the algorithm is 0 for RSA PKCS#1 v1.5 with SHA-256 or 1 for ECDSA with SHA-256 (DER or raw r||s). The options of nexpdf_complete_external_signature take {"tsa_url": "...", "ltv": true} and also "level", so an external signature can be B-LTA too.

Long-term archival

For PAdES B-LTA, document timestamps and archive timestamp renewal, see Long-term signatures. Document timestamps have Dart methods (doc.addDocumentTimestamp, sdk.addDocumentTimestampBytes).

On this page