Rust
Rust
Create Egypt ITIDA CAdES-BES .p7s Signature (File to File)
See more Egypt ITIDA Examples
Demonstrates how to create a .p7s signature that fits Egypt's ITIDA requirements.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();
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, which does not contain the data being signed.
let path_of_file_to_sign = "someDir/someFile".to_string();
let output_path = "outDir/someFile.p7s".to_string();
if crypt.create_p7_s(&path_of_file_to_sign, &output_path).is_err() {
println!("{}", crypt.last_error_text());
return;
}
println!("Success!");