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

Get a Certificate's Key Size

See more Certificates Examples

Demonstrates how to get the RSA key size of a certificate (for example, 1024-bit, 2048-bit, etc.)

Chilkat React Native Downloads

React Native
import { BinData, Cert, PublicKey, StringBuilder, Xml } from '@chilkat/react-native'

function chilkatExample() {
  // For this example, I have a certificate in raw base64 format (not PEM),
  // that looks like this:  "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
  const sbCertBase64 = new StringBuilder();
  sbCertBase64.loadFile('qa_data/certs/base64Cert.txt', 'utf-8');

  const cert = new Cert();
  try {
    cert.loadFromBase64(sbCertBase64.getAsString());
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  // Get the public key.
  const pubKey = new PublicKey();
  cert.getPublicKey(pubKey);

  let numBits = pubKey.keySize;
  console.log(`Number of bits = ${numBits}`);

  // If using an older version of Chilkat, the key size can be obtained like this:
  const xml = new Xml();
  xml.loadXml(pubKey.getXml());

  const binDat = new BinData();
  binDat.appendEncoded(xml.getChildContent('Modulus'), 'base64');

  numBits = 8 * binDat.numBytes;
  console.log(`Number of bits = ${numBits}`);
}