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

Verify a signed message with a trusted public key file

See more DKIM Examples

Use the saved signed message and its matching public PEM file to test a signing configuration before publishing DNS. This verifier caches the key for the exact selector and domain. A different pair can trigger normal DNS lookup, so the example also checks which signature actually verified.

Load the key from a trusted source independent of the received message. A pass with a locally supplied key confirms the cryptographic match. Use the published-key check to test what recipients can retrieve.

Chilkat Xbase++ Downloads

Xbase++
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:LoadPublicKeyFile(cSelector, cDomain, "dkim-public.pem")
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

? "Signature verified with the saved key for " + cSelector + "._domainkey." + cDomain

oMessage:destroy()
oVerifier:destroy()
oInfo:destroy()