Rust Requires Chilkat v11.0.0+
Rust
RSA Signature with Certificate's Private Key from PFX
See more RSA Examples
Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
let cert_store = chilkat::CertStore::new();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
if cert_store.load_pfx_file("chilkat.pfx", "test").is_err() {
println!("{}", cert_store.last_error_text());
return;
}
// Find the certificate by the subject common name:
let json_cn = chilkat::JsonObject::new();
let _ = json_cn.update_string("CN", "cert common name");
let cert = chilkat::Cert::new();
if cert_store.find_cert(&json_cn, &cert).is_err() {
println!("{}", cert_store.last_error_text());
return;
}
let priv_key = chilkat::PrivateKey::new();
if cert.get_private_key(&priv_key).is_err() {
println!("{}", cert.last_error_text());
return;
}
let rsa = chilkat::Rsa::new();
if rsa.use_private_key(&priv_key).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// Encode the signature as a hex string
rsa.set_encoding_mode("hex");
let str_data = "This is the string to be signed.".to_string();
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "sha-256", "md2" and "md5".
let hex_sig = rsa.sign_string_enc(&str_data, "sha-1").unwrap_or_default();
println!("{}", hex_sig);
println!("Success!");