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

Duplicate openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat

See more OpenSSL Examples

Demonstrates how to duplicate this OpenSSL command:
openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat
The in.dat file can contain text or binary data of any type. The OpenSSL command does the following:
  1. Creates a SHA256 digest of the contents of the input file
  2. Signs the SHA256 digest using the private key.

Chilkat Rust Downloads

Rust

// This example requires 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 PEM file:
if pkey.load_pem_file("private.pem").is_err() {
    println!("{}", pkey.last_error_text());
    return;
}

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

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

// OpenSSL uses big-endian.
rsa.set_little_endian(false);

// Load the file to be signed.
let bd_file_data = chilkat::BinData::new();
let _ = bd_file_data.load_file("in.dat").is_ok();

let bd_sig = chilkat::BinData::new();
if rsa.sign_bd(&bd_file_data, "sha256", &bd_sig).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// Save the binary signature to a file.
if bd_sig.write_file("signature.sig").is_err() {
    println!("Failed to write signature.sig.");
    return;
}

println!("Success.");