Sample code for 30+ languages & platforms
DataFlex

Create a DKIM-Signature for a MIME Message

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to a complete MIME message held in a BinData, modifying it in place. The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount properties configure the generated signature.

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: The signature is computed over a hash of the body plus a chosen set of headers, so the message must be completely built — every header, encoding, and boundary — before signing; any later change invalidates it. The d= (domain) and s= (selector) tags tell a verifier where to find the public key: at selector._domainkey.domain in DNS. relaxed/relaxed canonicalization tolerates the minor whitespace changes mail systems make in transit, which is why it is the common choice.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoDkim
    Variant vPrivKey
    Handle hoPrivKey
    Variant vMimeData
    Handle hoMimeData
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Dkim.DkimSign method, which creates a DKIM-Signature header and prepends it to
    //  a complete MIME message.  The only argument is a BinData holding the MIME, which is modified in
    //  place.
    //  
    //  The DkimAlg, DkimCanon, DkimDomain, DkimSelector, DkimHeaders, and DkimBodyLengthCount
    //  properties configure the signature that is generated.

    Get Create (RefClass(cComChilkatDkim)) To hoDkim
    If (Not(IsComObjectCreated(hoDkim))) Begin
        Send CreateComObject of hoDkim
    End

    //  Configure the DKIM signature.
    Set ComDkimDomain Of hoDkim To "example.com"
    Set ComDkimSelector Of hoDkim To "myselector"

    //  Signing algorithm written to the a= tag.
    Set ComDkimAlg Of hoDkim To "rsa-sha256"

    //  Canonicalization for headers and body, written to the c= tag.
    Set ComDkimCanon Of hoDkim To "relaxed/relaxed"

    //  Colon-separated list of header fields to sign, written to the h= tag.
    Set ComDkimHeaders Of hoDkim To "From:To:Subject:Date:Message-ID"

    //  Maximum number of canonicalized body bytes to hash.  0 means the entire body.
    Set ComDkimBodyLengthCount Of hoDkim To 0

    //  Load the RSA private key and give it to the Dkim object.
    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get ComLoadPemFile Of hoPrivKey "qa_data/dkim_private.pem" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPrivKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoPrivKey to vPrivKey
    Get ComSetDkimPrivateKey Of hoDkim vPrivKey To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoDkim To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Load the complete MIME message to be signed.  It must already contain all headers, transfer
    //  encodings, MIME boundaries, and body bytes.
    Get Create (RefClass(cComChilkatBinData)) To hoMimeData
    If (Not(IsComObjectCreated(hoMimeData))) Begin
        Send CreateComObject of hoMimeData
    End
    Get ComLoadFile Of hoMimeData "qa_data/message.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMimeData To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Sign.  The DKIM-Signature header is prepended to the MIME in place.
    Get pvComObject of hoMimeData to vMimeData
    Get ComDkimSign Of hoDkim vMimeData To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoDkim To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Save the signed message.
    Get ComWriteFile Of hoMimeData "qa_output/signed.eml" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMimeData To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Message signed."


End_Procedure