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

Sign a saved MIME message with DKIM

See more DKIM Examples

Sign the complete bytes of message.eml and save the result as signed.eml. Finish the message’s From, To, Date, Message-ID, MIME parts and transfer encodings before signing. Existing signatures stay in place and the new signature is inserted first.

The example signs the full body and includes From in the signed headers. Relaxed canonicalization tolerates defined whitespace and header folding changes. Editing a signed field or changing the body text can still break verification. Prepare the matching DNS record.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vMessage
    Handle hoMessage
    Variant vPrivateKey
    Handle hoPrivateKey
    Handle hoSigner
    String sTemp1

    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 "message.eml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read message.eml."
        Procedure_Return
    End

    //  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivateKey
    If (Not(IsComObjectCreated(hoPrivateKey))) Begin
        Send CreateComObject of hoPrivateKey
    End
    Get ComLoadAnyFormatFile Of hoPrivateKey "dkim-private.pem" "" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPrivateKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatDkim)) To hoSigner
    If (Not(IsComObjectCreated(hoSigner))) Begin
        Send CreateComObject of hoSigner
    End
    Set ComDkimDomain Of hoSigner To "example.com"
    Set ComDkimSelector Of hoSigner To "mail2026"
    Set ComDkimAlg Of hoSigner To "rsa-sha256"
    Get pvComObject of hoPrivateKey to vPrivateKey
    Get ComSetDkimPrivateKey Of hoSigner vPrivateKey To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSigner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  DkimCanon takes a single setting and applies it to both headers and body.
    Set ComDkimCanon Of hoSigner To "relaxed"
    Set ComDkimHeaders Of hoSigner To "from:to:subject:date:message-id:mime-version:content-type:content-transfer-encoding"
    //  Zero signs the entire canonicalized body and omits the optional l= tag.
    Set ComDkimBodyLengthCount Of hoSigner To 0
    Get pvComObject of hoMessage to vMessage
    Get ComDkimSign Of hoSigner vMessage To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoSigner To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComWriteFile Of hoMessage "signed.eml" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not write signed.eml."
        Procedure_Return
    End

    Showln "Saved signed.eml."


End_Procedure