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

Sign an email with a key in the Windows Current User certificate store

See more DKIM Examples

Run on Windows as the account that owns the signing certificate. Copy its thumbprint from Current User > Personal and supply it as hexadecimal digits. The thumbprint selects a certificate. The DKIM signature itself uses RSA-SHA256.

The certificate needs an accessible, exportable RSA private key for GetPrivateKey. Publish its matching public key under the selector in DNS. DKIM recipients use that DNS key to verify the message. An X.509 chain is unnecessary for this step.

Chilkat Ruby Downloads

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

store = Chilkat::CkCertStore.new()
success = store.OpenCurrentUserStore(true)
if (success != true)
    print store.lastErrorText() + "\n";
    exit
end

search = Chilkat::CkJsonObject.new()
search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT")
cert = Chilkat::CkCert.new()
success = store.FindCert(search,cert)
if (success != true)
    print store.lastErrorText() + "\n";
    exit
end

if (cert.HasPrivateKey() != true)
    print "The selected certificate has no accessible private key." + "\n";
    exit
end

privateKey = Chilkat::CkPrivateKey.new()
success = cert.GetPrivateKey(privateKey)
if (success != true)
    print cert.lastErrorText() + "\n";
    exit
end

signer = Chilkat::CkDkim.new()
signer.put_DkimDomain("example.com")
signer.put_DkimSelector("mail2026")
signer.put_DkimAlg("rsa-sha256")
success = signer.SetDkimPrivateKey(privateKey)
if (success != true)
    print signer.lastErrorText() + "\n";
    exit
end

message = Chilkat::CkBinData.new()
success = message.LoadFile("message.eml")
if (success != true)
    print "Could not read message.eml." + "\n";
    exit
end

success = signer.DkimSign(message)
if (success != true)
    print signer.lastErrorText() + "\n";
    exit
end

success = message.WriteFile("signed.eml")
if (success != true)
    print "Could not write signed.eml." + "\n";
    exit
end

print "Saved signed.eml." + "\n";