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

Verify DKIM using a public key supplied as text

See more DKIM Examples

Applications that keep public keys in configuration can pass a complete PEM string or the base64 value of the DNS p= tag to LoadPublicKey. This example reads a trusted PEM as text so it can be run alongside the file-based example. Supply just the key material when passing a base64 value. Reuse the same verifier for signatures with that exact domain and selector.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Message
oleobject loo_Verifier
string ls_Domain
string ls_Selector
oleobject loo_KeyText
oleobject loo_Info

li_Success = 0

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

loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_Message
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Message.LoadFile("signed.eml")
if li_Success <> 1 then
    Write-Debug "Could not read signed.eml."
    destroy loo_Message
    return
end if

loo_Verifier = create oleobject
li_rc = loo_Verifier.ConnectToNewObject("Chilkat.Dkim")

ls_Domain = "example.com"
ls_Selector = "mail2026"
loo_KeyText = create oleobject
li_rc = loo_KeyText.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_KeyText.LoadFile("dkim-public.pem","utf-8")
if li_Success <> 1 then
    Write-Debug "Could not read the trusted public PEM."
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_KeyText
    return
end if

li_Success = loo_Verifier.LoadPublicKey(ls_Selector,ls_Domain,loo_KeyText.GetAsString())
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_KeyText
    return
end if

//  signed.eml from the signing example has our newly added signature at index zero.
if loo_Verifier.NumDkimSigs(loo_Message) = 0 then
    Write-Debug "No DKIM signature in signed.eml."
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_KeyText
    return
end if

li_Success = loo_Verifier.DkimVerify(0,loo_Message)
if li_Success <> 1 then
    Write-Debug loo_Verifier.LastErrorText
    Write-Debug loo_Verifier.VerifyInfo
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_KeyText
    return
end if

loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Info.Load(loo_Verifier.VerifyInfo)
if (li_Success <> 1) OR (loo_Info.StringOfEquals("domain",ls_Domain,1) <> 1) OR (loo_Info.StringOfEquals("selector",ls_Selector,1) <> 1) then
    Write-Debug "The first signature is not from the selected domain and selector."
    destroy loo_Message
    destroy loo_Verifier
    destroy loo_KeyText
    destroy loo_Info
    return
end if

Write-Debug "The configured public key verifies this message."


destroy loo_Message
destroy loo_Verifier
destroy loo_KeyText
destroy loo_Info