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

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = false;

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

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

    var search = new chilkat.JsonObject();
    search.UpdateString("thumbprint","YOUR_CERTIFICATE_THUMBPRINT");
    var cert = new chilkat.Cert();
    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 chilkat.PrivateKey();
    success = cert.GetPrivateKey(privateKey);
    if (success !== true) {
        console.log(cert.LastErrorText);
        return;
    }

    var signer = new chilkat.Dkim();
    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 chilkat.BinData();
    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.");

}

chilkatExample();