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

RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature

See more RSA Examples

Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.

Chilkat Rust Downloads

Rust

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

let rsa = chilkat::Rsa::new();

// Load the .pfx/.p12
let pfx = chilkat::Pfx::new();
if pfx.load_pfx_file("qa_data/pfx/myKey.p12", "myPassword").is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

// Get the default private key.

let priv_key = chilkat::PrivateKey::new();
if pfx.private_key_at(0, &priv_key).is_err() {
    println!("{}", pfx.last_error_text());
    return;
}

// Import the private key into the RSA component:
if rsa.use_private_key(&priv_key).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// Get the signature in base64
rsa.set_encoding_mode("base64");

let str_data = "This is the string to be signed.".to_string();

// Sign the string using the sha256 hash algorithm.
// Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
let base64_sig = rsa.sign_string_enc(&str_data, "sha256").unwrap_or_default();

println!("{}", base64_sig);

println!("Success!");