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

Iterate Keys and Certs in PEM

See more PEM Examples

Demonstrates how to access each of the private keys and certs contained within a PEM.

Chilkat React Native Downloads

React Native
import { Cert, Pem, PrivateKey } from '@chilkat/react-native'

function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const pem = new Pem();

  // Load the PEM from a file.
  // If the PEM is encrypted, provide a password.  Otherwise pass an empty string for the password.
  const password = 'myPassword';
  try {
    pem.loadPemFile('../myPemFiles/myPem.pem', password);
  } catch {
    console.log(pem.lastErrorText);
    return;
  }

  // Note: If the app already has the PEM pre-loaded in a string variable, then load it 
  // by calling LoadPem instead.  
  const pemContent = '... the PEM contents ...';
  pem.loadPem(pemContent, password);
  // Check for success as before..

  // Iterate over the private keys.
  const numPrivateKeys = pem.numPrivateKeys;
  let i = 0;

  const privKey = new PrivateKey();
  while (i < numPrivateKeys) {
    pem.privateKeyAt(i, privKey);
    console.log(`Private Key ${i} is ${privKey.bitLength} in length`);
    i++;
  }

  // Iterate over the certificates.
  const cert = new Cert();
  const numCerts = pem.numCerts;
  i = 0;
  while (i < numCerts) {
    pem.certAt(i, cert);
    console.log(`Certificate ${i} : ${cert.subjectDN}`);
    i++;
  }
}