Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vMessage
    Handle hoMessage
    Handle hoVerifier
    String sDomain
    String sSelector
    Handle hoInfo
    String sTemp1
    Integer iTemp1
    Boolean bTemp1
    Boolean bTemp2

    Move False To iSuccess

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    Get Create (RefClass(cComChilkatBinData)) To hoMessage
    If (Not(IsComObjectCreated(hoMessage))) Begin
        Send CreateComObject of hoMessage
    End
    Get ComLoadFile Of hoMessage "signed.eml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read signed.eml."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatDkim)) To hoVerifier
    If (Not(IsComObjectCreated(hoVerifier))) Begin
        Send CreateComObject of hoVerifier
    End
    Move "example.com" To sDomain
    Move "mail2026" To sSelector
    Get ComLoadPublicKeyFile Of hoVerifier sSelector sDomain "dkim-public.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  signed.eml from the signing example has our newly added signature at index zero.
    Get ComNumDkimSigs Of hoVerifier     Get pvComObject of hoMessage to vMessage
vMessage To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No DKIM signature in signed.eml."
        Procedure_Return
    End

    Get pvComObject of hoMessage to vMessage
    Get ComDkimVerify Of hoVerifier 0 vMessage To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
        Get ComVerifyInfo Of hoVerifier To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoInfo
    If (Not(IsComObjectCreated(hoInfo))) Begin
        Send CreateComObject of hoInfo
    End
    Get ComVerifyInfo Of hoVerifier To sTemp1
    Get ComLoad Of hoInfo sTemp1 To iSuccess
    Get ComStringOfEquals Of hoInfo "domain" sDomain True To bTemp1
    Get ComStringOfEquals Of hoInfo "selector" sSelector True To bTemp2
    If ((iSuccess <> True) Or (bTemp1 <> True) Or (bTemp2 <> True)) Begin
        Showln "The first signature is not from the selected domain and selector."
        Procedure_Return
    End

    Showln "Signature verified with the saved key for " sSelector "._domainkey." sDomain


End_Procedure