Rust Requires Chilkat v11.0.0+
Rust
RSA Sign with PKCS8 Encrypted Key
See more RSA Examples
Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let pkey = chilkat::PrivateKey::new();
// Load the private key from an RSA PEM file:
let _ = pkey.load_pkcs8_encrypted_file("privateKey.key", "myPassword").is_ok();
let rsa = chilkat::Rsa::new();
// Import the private key into the RSA object:
if rsa.use_private_key(&pkey).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// Return the signature in hex
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 "md2", "sha256", "sha384", "sha512", and "md5".
let hex_sig = rsa.sign_string_enc(&str_data, "sha-1").unwrap_or_default();
println!("{}", hex_sig);