Verify DomainKey-Signature Headers in Downloaded Email
See more DKIM / DomainKey Examples
Downloads email from an IMAP server and verifies the DomainKey-Signature header(s) in each email, if present.
Note: DKIM-Signatures are much more common than DomainKey-Signatures. See the other Chilkat example for verifying DKIM-Signatures (link in the code below).
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const imap = try chilkat.Imap.init();
defer imap.deinit();
// Connect to an IMAP server, login, select mailbox..
// Use TLS
imap.setSsl(true);
imap.setPort(993);
success = if (imap.connect("imap.example.com")) true else |_| false;
if (success) {
success = if (imap.login("myLogin", "myPassword")) true else |_| false;
if (success) {
success = if (imap.selectMailbox("Inbox")) true else |_| false;
}
}
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// Note: DKIM-Signatures are much more common than DomainKey-Signature
// See DKIM-Signature Verify Sample.
const dkim = try chilkat.Dkim.init();
defer dkim.deinit();
// Download a max of 10 emails and verify any DomainKey-Signature headers
// that are present.
// Download emails by sequence numbers (not UIDs).
const b_uid = false;
var seq_num: i32 = 0;
var j: i32 = 0;
var n: i32 = imap.getNumMessages();
if (n > 50) {
n = 50;
}
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.setEmitCompact(false);
// To verify DomainKey-Signature headers, we need the exact unmodified MIME bytes of each email.
const mime_data = try chilkat.BinData.init();
defer mime_data.deinit();
seq_num = 1;
while (seq_num <= n) {
// The FetchSingleBd method was introduced in v9.5.0.76
success = if (imap.fetchSingleBd(@as(u32, @intCast(seq_num)), b_uid, mime_data)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
return;
}
// Note: DKIM-Signatures are much more common than DomainKey-Signature
// See DKIM-Signature Verify Sample.
// Get the number of DomainKey-Signature headers.
ERROR: Method not in the Zig package: Dkim.NumDomainKeySigs
ERROR: Method not in the Zig package: Dkim.NumDomainKeySigs
const num_sigs = dkim.ERROR();
// Verify each..
j = 0;
while (j < num_sigs) {
std.debug.print("------ DomainKey Signature {d}\n", .{j});
ERROR: Method not in the Zig package: Dkim.DomainKeyVerify
ERROR: Method not in the Zig package: Dkim.DomainKeyVerify
success = dkim.ERROR();
if (!success) {
std.debug.print("Not valid.\n", .{});
std.debug.print("{s}\n", .{try dkim.getLastErrorText(alloc)});
} else {
std.debug.print("valid.\n", .{});
}
// Show the additional information about the signature verification
json.load(try dkim.getVerifyInfo(alloc)) catch {};
std.debug.print("{s}\n", .{try json.emit(alloc)});
// The JSON contains information such as this:
// {
// "domain": "amazonses.com",
// "selector": "7v7vs6w47njt4pimodk5mmttbegzsi6n",
// "publicKey": "MIGfMA0GCSqG...v2GvWPqGHz6uqeQIDAQAB",
// "canonicalization": "relaxed/simple",
// "algorithm": "rsa-sha256",
// "signedHeaders": "Subject:From:To:Date:Mime-Version:Content-Type:References:Message-Id:Feedback-ID",
// "verified": "yes"
// }
j = j + 1;
}
seq_num = seq_num + 1;
}
success = if (imap.disconnect()) true else |_| false;
}