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

Verify the RSA Signature of a SHA256 Hash

See more RSA Examples

Demonstrates how to verify an RSA signature of a SHA256 hash.

Chilkat Rust Downloads

Rust

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

// Let's say you have a file containing the 32-bytes of a SHA256 hash,
// and a file that is an RSA signature of those 32 bytes.
// Here's how you verify using the RSA public key found in a PEM.

let pub_key = chilkat::PublicKey::new();
if pub_key.load_from_file("rsaPubKey.pem").is_err() {
    println!("{}", pub_key.last_error_text());
    return;
}

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

// Get the public key.
if rsa.use_public_key(&pub_key).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// Get the 32-byte SHA256 hash.
let bd_hash = chilkat::BinData::new();
if bd_hash.load_file("myHash.sha256").is_err() {
    println!("Failed to load SHA256 hash.");
    return;
}

// Get the RSA signature to be validated.
let bd_sig = chilkat::BinData::new();
if bd_sig.load_file("mySig.sig").is_err() {
    println!("Failed to load RSA signature.");
    return;
}

// Verify the signature against the SHA256 hash.
let enc = "base64".to_string();
rsa.set_encoding_mode(&enc);
if rsa.verify_hash_enc(&bd_hash.get_encoded(&enc).unwrap_or_default(), "sha256", &bd_sig.get_encoded(&enc).unwrap_or_default()).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

println!("Signature validated.");