Sample code for 30+ languages & platforms
Rust Requires Chilkat v10.1.2+

RSA Sign String using Private Key of Certificate Type A3 (smart card / token)

See more RSA Examples

Demonstrates RSA signing a string using the private key of a certificate type A3 (smart card, token).

Note: This is a Windows-only example.

Chilkat Rust Downloads

Rust

// First get the A3 certificate that was installed on the Windows system.
let cert_store = chilkat::CertStore::new();

let thumbprint = "12c1dd8015f3f03f7b1fa619dc24e2493ca8b4b2".to_string();

// This is specific to Windows because it is opening the Windows Current-User certificate store.
let b_read_only = true;
if cert_store.open_current_user_store(b_read_only).is_err() {
    println!("{}", cert_store.last_error_text());
    return;
}

// Find the certificate with the desired thumbprint
// (There are many ways to locate a certificate.  This example chooses to find by thumbprint.)
let json = chilkat::JsonObject::new();
let _ = json.update_string("thumbprint", &thumbprint);

let cert = chilkat::Cert::new();
if cert_store.find_cert(&json, &cert).is_err() {
    println!("Failed to find the certificate.");
    return;
}

println!("Found: {}", cert.subject_cn());

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

// Provide the cert's private key
let b_use_private_key = true;
if rsa.set_x509_cert(&cert, b_use_private_key).is_err() {
    println!("{}", rsa.last_error_text());
    return;
}

// Return the RSA signature in base64 encoded form.
rsa.set_encoding_mode("base64");

// Sign the utf-8 byte representation of the string.
rsa.set_charset("utf-8");

// You can also choose other hash algorithms, such as SHA-1.
let Ok(sig_base64) = rsa.sign_string_enc("text to sign", "SHA-256") else {
    println!("{}", rsa.last_error_text());
    return;
};

println!("Base64 signature: {}", sig_base64);