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

Rust

// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this:  "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
let sb_cert_base64 = chilkat::StringBuilder::new();
let _ = sb_cert_base64.load_file("qa_data/certs/base64Cert.txt", "utf-8").is_ok();

let cert = chilkat::Cert::new();
if cert.load_from_base64(&sb_cert_base64.get_as_string().unwrap_or_default()).is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// Get the public key.
let pub_key = chilkat::PublicKey::new();
let _ = cert.get_public_key(&pub_key);

let mut num_bits = pub_key.key_size();
println!("Number of bits = {}", num_bits);

// If using an older version of Chilkat, the key size can be obtained like this:
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&pub_key.get_xml().unwrap_or_default());

let bin_dat = chilkat::BinData::new();
let _ = bin_dat.append_encoded(&xml.get_child_content("Modulus").unwrap_or_default(), "base64");

num_bits = 8 * bin_dat.num_bytes();
println!("Number of bits = {}", num_bits);