DataFlex
DataFlex
Verify DKIM Signatures in a Message
See more DKIM / DomainKey Examples
Demonstrates the Chilkat Dkim.DkimVerify method, which verifies the DKIM-Signature at a given zero-based index in a MIME message. The arguments are the signature index and a BinData holding the complete MIME. This example loops over every signature and reads the JSON VerifyInfo after each.
Tip: JSON parsing code for the VerifyInfo result can be generated at Chilkat Tools.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: Verifying confirms two things at once: that the message was signed by the claimed domain, and that the signed headers and body have not been altered since. Chilkat uses a cached public key when the selector and domain match one you loaded, and otherwise fetches it from DNS. The message is not modified. After each call, the
VerifyInfo JSON records the details — selector, domain, and outcome — which is what you would log or surface when a signature fails. Verification is per-signature, so a message with several signatures may have some pass and others fail.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoDkim
Variant vMimeData
Handle hoMimeData
Integer iNumSigs
Integer i
String sTemp1
Move False To iSuccess
// Demonstrates the Dkim.DkimVerify method, which verifies the DKIM-Signature at a given index in
// a MIME message. The 1st argument is the zero-based signature index and the 2nd is a BinData
// holding the complete MIME.
Get Create (RefClass(cComChilkatDkim)) To hoDkim
If (Not(IsComObjectCreated(hoDkim))) Begin
Send CreateComObject of hoDkim
End
// Load the received MIME message.
Get Create (RefClass(cComChilkatBinData)) To hoMimeData
If (Not(IsComObjectCreated(hoMimeData))) Begin
Send CreateComObject of hoMimeData
End
Get ComLoadFile Of hoMimeData "qa_data/received.eml" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoMimeData To sTemp1
Showln sTemp1
Procedure_Return
End
// A message may carry more than one DKIM signature. Verify each in turn. Signatures are
// indexed in top-to-bottom header order.
Get pvComObject of hoMimeData to vMimeData
Get ComNumDkimSigs Of hoDkim vMimeData To iNumSigs
For i From 0 To (iNumSigs - 1)
// DkimVerify uses a cached public key when one matches the signature's selector and domain;
// otherwise it retrieves the key from DNS.
Get pvComObject of hoMimeData to vMimeData
Get ComDkimVerify Of hoDkim i vMimeData To iSuccess
If (iSuccess = True) Begin
Showln "Signature " i ": verified."
End
Else Begin
Showln "Signature " i ": verification failed."
End
// VerifyInfo contains JSON describing the most recent DkimVerify call (selector, domain,
// result, and so on).
Get ComVerifyInfo Of hoDkim To sTemp1
Showln sTemp1
// JSON parsing code for this result can be generated at Chilkat's online tool:
// https://tools.chilkat.io/jsonParse
Loop
End_Procedure