Swift Requires Chilkat v11.0.0+
Swift
Verify each DKIM signature in email downloaded over IMAP
See more DKIM Examples
Download the first ten messages (or all messages in a smaller inbox) as complete MIME, including attachments, while preserving their unread flags. The verifier looks up the selector’s TXT record when it needs a key. Inspect VerifyInfo immediately after each attempt for the signing domain, selector, and any body-hash mismatch.
Verify each signature independently, in top-to-bottom order. A passing signature authenticates the signing domain’s responsibility for the signed content. Identifying the author and checking DMARC alignment with the visible From address are separate steps. A missing DNS key or unavailable resolver can also prevent verification. DKIM specification.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let imap = CkoImap()!
imap.ssl = true
imap.port = 993
imap.requireSslCertVerify = true
imap.peekMode = true
imap.autoDownloadAttachments = true
success = imap.connect(hostname: "imap.example.com")
if success != true {
print("\(imap.lastErrorText!)")
return
}
success = imap.login(login: "IMAP_USERNAME", password: "IMAP_PASSWORD")
if success != true {
print("\(imap.lastErrorText!)")
return
}
success = imap.selectMailbox(mailbox: "INBOX")
if success != true {
print("\(imap.lastErrorText!)")
return
}
// Select the first ten sequence positions, then fetch by stable UID.
let messages = CkoMessageSet()!
success = imap.queryMbx(criteria: "1:10", bUid: true, msgSet: messages)
if success != true {
print("\(imap.lastErrorText!)")
return
}
var i: Int = 0
let verifier = CkoDkim()!
let message = CkoBinData()!
while (i < messages.count.intValue) {
var uid: UInt32 = messages.getId(index: i)
print("Message UID \(uid)")
success = imap.fetchSingleBd(msgId: uid, bUid: true, mimeData: message)
if success != true {
print("\(imap.lastErrorText!)")
}
else {
var count: Int = verifier.numSigs(mimeData: message).intValue
if count == 0 {
print("No DKIM signatures.")
}
var sig: Int = 0
while (sig < count) {
success = verifier.dkimVerify(sigIndex: sig, mimeData: message)
// Read diagnostics before the next verification replaces them.
let info = CkoJsonObject()!
var haveInfo: Bool = info.load(json: verifier.verifyInfo)
if success == true {
print("Signature \(sig) verified.")
}
else {
if (haveInfo == true) && (info.string(ofEquals: "bodyHashVerified", value: "no", caseSensitive: true) == true) {
print("Signature \(sig): the body hash differs.")
}
else {
print("Signature \(sig) could not be verified.")
}
print("\(verifier.lastErrorText!)")
}
if (haveInfo == true) && (info.jsonType(of: "domain").intValue == 1) {
print("Signing domain: \(info.string(of: "domain")!) Selector: \(info.string(of: "selector")!)")
}
sig = sig + 1
}
}
i = i + 1
}
success = imap.disconnect()
}