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

Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

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

var store = new CkCertStore();
success = store.OpenCurrentUserStore(true);
if (success !== true) {
    console.log(store.LastErrorText);
    return;
}

var search = new CkJsonObject();
search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT");
var cert = new CkCert();
success = store.FindCert(search,cert);
if (success !== true) {
    console.log(store.LastErrorText);
    return;
}

if (cert.HasPrivateKey() !== true) {
    console.log("The selected certificate has no accessible private key.");
    return;
}

var privateKey = new CkPrivateKey();
success = cert.GetPrivateKey(privateKey);
if (success !== true) {
    console.log(cert.LastErrorText);
    return;
}

var signer = new CkDkim();
signer.DkimDomain = "example.com";
signer.DkimSelector = "mail2026";
signer.DkimAlg = "rsa-sha256";
success = signer.SetDkimPrivateKey(privateKey);
if (success !== true) {
    console.log(signer.LastErrorText);
    return;
}

var message = new CkBinData();
success = message.LoadFile("message.eml");
if (success !== true) {
    console.log("Could not read message.eml.");
    return;
}

success = signer.DkimSign(message);
if (success !== true) {
    console.log(signer.LastErrorText);
    return;
}

success = message.WriteFile("signed.eml");
if (success !== true) {
    console.log("Could not write signed.eml.");
    return;
}

console.log("Saved signed.eml.");