Rust
Rust
Create Egypt ITIDA CAdES-BES .p7s Signature (with strings in-memory)
See more Egypt ITIDA Examples
Demonstrates how to create a .p7s signature that fits Egypt's ITIDA requirements.Note: This example requires Chilkat v9.5.0.75 or greater.
Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let crypt = chilkat::Crypt2::new();
let cert = chilkat::Cert::new();
// There are many ways to load the certificate.
// This example was created for a customer using an ePass2003 USB token.
// Assuming the USB token is the only source of a hardware-based private key..
if cert.load_from_smartcard("").is_err() {
println!("{}", cert.last_error_text());
return;
}
// Tell the crypt component to use this cert.
if crypt.set_signing_cert(&cert).is_err() {
println!("{}", crypt.last_error_text());
return;
}
let cms_options = chilkat::JsonObject::new();
// Setting "DigestData" causes OID 1.2.840.113549.1.7.5 (digestData) to be used.
let _ = cms_options.update_bool("DigestData", true);
let _ = cms_options.update_bool("OmitAlgorithmIdNull", true);
crypt.set_cms_options(&cms_options.emit().unwrap_or_default());
// The CadesEnabled property applies to all methods that create CMS/PKCS7 signatures.
// To create a CAdES-BES signature, set this property equal to true.
crypt.set_cades_enabled(true);
crypt.set_hash_algorithm("sha256");
let json_signing_attrs = chilkat::JsonObject::new();
let _ = json_signing_attrs.update_int("contentType", 1);
let _ = json_signing_attrs.update_int("signingTime", 1);
let _ = json_signing_attrs.update_int("messageDigest", 1);
let _ = json_signing_attrs.update_int("signingCertificateV2", 1);
crypt.set_signing_attributes(&json_signing_attrs.emit().unwrap_or_default());
// By default, all the certs in the chain of authentication are included in the signature.
// If desired, we can choose to only include the signing certificate:
crypt.set_include_cert_chain(false);
// Make sure we sign the utf-8 byte representation of the JSON string
crypt.set_charset("utf-8");
// Create the CAdES-BES signature.
let text_to_sign = "\"issuer\"\"address\"\"branchID\"\"0\"\"country\"\"EG\"\"regionCity...".to_string();
crypt.set_encoding_mode("base64");
let Ok(sig_base64) = crypt.sign_string_enc(&text_to_sign) else {
println!("{}", crypt.last_error_text());
return;
};
println!("Base64 signature:");
println!("{}", sig_base64);