Xbase++ Requires Chilkat v11.0.0+
Xbase++
Check a published DKIM selector against a signed message
See more DKIM Examples
After publishing a selector, run this check with a message signed by its matching private key. Start with a fresh verifier, fetch the public key from DNS, and then verify the message with that same object. DNS lookup alone shows only that a usable key was returned. Signature verification also checks that it matches what the signer used.
Set the signing domain and selector exactly as they appear in the signature. Applications processing many messages can reuse the verifier for that selector. After a key change, use a new verifier and allow for the DNS resolver’s TTL.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oMessage
LOCAL oVerifier
LOCAL cDomain
LOCAL cSelector
LOCAL oInfo
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oMessage := CreateObject("Chilkat.BinData")
nSuccess := oMessage:LoadFile("signed.eml")
IF (nSuccess != 1)
? "Could not read signed.eml."
oMessage:destroy()
RETURN
ENDIF
oVerifier := CreateObject("Chilkat.Dkim")
cDomain := "example.com"
cSelector := "mail2026"
nSuccess := oVerifier:PrefetchPublicKey(cSelector, cDomain)
IF (nSuccess != 1)
? oVerifier:LastErrorText
oMessage:destroy()
oVerifier:destroy()
RETURN
ENDIF
// signed.eml from the signing example has our newly added signature at index zero.
IF (oVerifier:NumDkimSigs(oMessage) == 0)
? "No DKIM signature in signed.eml."
oMessage:destroy()
oVerifier:destroy()
RETURN
ENDIF
nSuccess := oVerifier:DkimVerify(0, oMessage)
IF (nSuccess != 1)
? oVerifier:LastErrorText
? oVerifier:VerifyInfo
oMessage:destroy()
oVerifier:destroy()
RETURN
ENDIF
oInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oInfo:Load(oVerifier:VerifyInfo)
IF ((nSuccess != 1) .OR. (oInfo:StringOfEquals("domain", cDomain, 1) != 1) .OR. (oInfo:StringOfEquals("selector", cSelector, 1) != 1))
? "The first signature is not from the selected domain and selector."
oMessage:destroy()
oVerifier:destroy()
oInfo:destroy()
RETURN
ENDIF
? "The published key verifies this message for " + cSelector + "._domainkey." + cDomain
oMessage:destroy()
oVerifier:destroy()
oInfo:destroy()