Sample code for 30+ languages & platforms
JavaScript

ZATCA Load Certificate and Private Key from PEM Files

See more ZATCA Examples

Demonstrates how to load a certificate and private key from a pair of PEM files.
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;

// The LoadFromFile method will automatically detect the file format..
var cert = new CkCert();
success = cert.LoadFromFile("qa_data/zatca/cert.pem");
if (success !== true) {
    console.log(cert.LastErrorText);
    return;
}

console.log(cert.SubjectCN);

// Load the private key.
var privKey = new CkPrivateKey();
success = privKey.LoadPemFile("qa_data/zatca/ec-secp256k1-priv-key.pem");
if (success !== true) {
    console.log(privKey.LastErrorText);
    return;
}

console.log("Key Type: " + privKey.KeyType);

// Associate the private key with the certificate.
success = cert.SetPrivateKey(privKey);
if (success !== true) {
    console.log(cert.LastErrorText);
    return;
}

console.log("Success.");