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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkDkim.pb"
IncludeFile "CkPrivateKey.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

    message.i = CkBinData::ckCreate()
    If message.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(message,"message.eml")
    If success <> 1
        Debug "Could not read message.eml."
        CkBinData::ckDispose(message)
        ProcedureReturn
    EndIf

    ;  Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
    privateKey.i = CkPrivateKey::ckCreate()
    If privateKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckLoadAnyFormatFile(privateKey,"dkim-private.pem","")
    If success <> 1
        Debug CkPrivateKey::ckLastErrorText(privateKey)
        CkBinData::ckDispose(message)
        CkPrivateKey::ckDispose(privateKey)
        ProcedureReturn
    EndIf

    signer.i = CkDkim::ckCreate()
    If signer.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkDkim::setCkDkimDomain(signer, "example.com")
    CkDkim::setCkDkimSelector(signer, "mail2026")
    CkDkim::setCkDkimAlg(signer, "rsa-sha256")
    success = CkDkim::ckSetDkimPrivateKey(signer,privateKey)
    If success <> 1
        Debug CkDkim::ckLastErrorText(signer)
        CkBinData::ckDispose(message)
        CkPrivateKey::ckDispose(privateKey)
        CkDkim::ckDispose(signer)
        ProcedureReturn
    EndIf

    ;  DkimCanon takes a single setting and applies it to both headers and body.
    CkDkim::setCkDkimCanon(signer, "relaxed")
    CkDkim::setCkDkimHeaders(signer, "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.
    CkDkim::setCkDkimBodyLengthCount(signer, 0)
    success = CkDkim::ckDkimSign(signer,message)
    If success <> 1
        Debug CkDkim::ckLastErrorText(signer)
        CkBinData::ckDispose(message)
        CkPrivateKey::ckDispose(privateKey)
        CkDkim::ckDispose(signer)
        ProcedureReturn
    EndIf

    success = CkBinData::ckWriteFile(message,"signed.eml")
    If success <> 1
        Debug "Could not write signed.eml."
        CkBinData::ckDispose(message)
        CkPrivateKey::ckDispose(privateKey)
        CkDkim::ckDispose(signer)
        ProcedureReturn
    EndIf

    Debug "Saved signed.eml."


    CkBinData::ckDispose(message)
    CkPrivateKey::ckDispose(privateKey)
    CkDkim::ckDispose(signer)


    ProcedureReturn
EndProcedure