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

Create PKCS1 RSA Signature with PEM Private Key

See more RSA Examples

Demonstrates how to load a private key from a PEM file and create a PKCS1 RSA digital signature.

Chilkat Rust Downloads

Rust

// 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:
if pkey.load_pem_file("pvkey2_rsa.pem").is_err() {
    println!("{}", pkey.last_error_text());
    return;
}

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;
}

// Get the signature as 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", "md5", "sha256",
// "sha384", and "sha512".
let hex_sig = rsa.sign_string_enc(&str_data, "sha-1").unwrap_or_default();

println!("{}", hex_sig);

println!("Success!");