Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

Verify DKIM using a public key supplied as text

See more DKIM Examples

Applications that keep public keys in configuration can pass a complete PEM string or the base64 value of the DNS p= tag to LoadPublicKey. This example reads a trusted PEM as text so it can be run alongside the file-based example. Supply just the key material when passing a base64 value. Reuse the same verifier for signatures with that exact domain and selector.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // Assumes Chilkat is unlocked. See Global Unlock Sample.

  final message = CkBinData();
  try {
    message.loadFile('signed.eml');
  } on ChilkatException {
    print('Could not read signed.eml.');
    return;
  }

  final verifier = CkDkim();
  final domain = 'example.com';
  final selector = 'mail2026';
  final keyText = CkStringBuilder();
  try {
    keyText.loadFile('dkim-public.pem', 'utf-8');
  } on ChilkatException {
    print('Could not read the trusted public PEM.');
    return;
  }

  try {
    verifier.loadPublicKey(selector, domain, keyText.getAsString());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // signed.eml from the signing example has our newly added signature at index zero.
  if (verifier.numDkimSigs(message) == 0) {
    print('No DKIM signature in signed.eml.');
    return;
  }

  try {
    verifier.dkimVerify(0, message);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    print(verifier.verifyInfo);
    return;
  }

  final info = CkJsonObject();
  success = true;
  try {
    info.load(verifier.verifyInfo);
  } on ChilkatException {
    success = false;
  }
  if ((!success) || ((!info.stringOfEquals('domain', domain, true)) || (!info.stringOfEquals('selector', selector, true)))) {
    print('The first signature is not from the selected domain and selector.');
    return;
  }

  print('The configured public key verifies this message.');
}