Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let pem = chilkat::Pem::new();
// Load the PEM from a file.
// If the PEM is encrypted, provide a password. Otherwise pass an empty string for the password.
let password = "myPassword".to_string();
if pem.load_pem_file("../myPemFiles/myPem.pem", &password).is_err() {
println!("{}", pem.last_error_text());
return;
}
// Note: If the app already has the PEM pre-loaded in a string variable, then load it
// by calling LoadPem instead.
let pem_content = "... the PEM contents ...".to_string();
let _ = pem.load_pem(&pem_content, &password).is_ok();
// Check for success as before..
// Iterate over the private keys.
let num_private_keys = pem.num_private_keys();
let mut i = 0;
let priv_key = chilkat::PrivateKey::new();
while i < num_private_keys {
let _ = pem.private_key_at(i, &priv_key);
println!("Private Key {} is {} in length", i, priv_key.bit_length());
i = i + 1;
}
// Iterate over the certificates.
let cert = chilkat::Cert::new();
let num_certs = pem.num_certs();
i = 0;
while i < num_certs {
let _ = pem.cert_at(i, &cert);
println!("Certificate {} : {}", i, cert.subject_dn());
i = i + 1;
}