Rust Requires Chilkat v11.0.0+
Rust
Sign Mexico Pedimento
See more Misc Examples
Add a signature to a Mexico pedimento file.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This is the contents before signing:
// 500|1|3621|4199800|400||
// 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
// 507|4199800|IM|2006-7888">
// 507|4199800|MS|2">
// 800|4199800|1">
// 801|M3621037.222|1|5|011|
// This is the contents after signing
// 500|1|3621|4199800|400||
// 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
// 507|4199800|IM|2006-7888">
// 507|4199800|MS|2">
// 800|4199800|1|fhP2Ker54D2+3+UZch23F0E72 .... 9qNSPIuAqpj524qLZbbA==|30001000000500003416|
// 801|M3621037.222|1|5|011|
// First create the text to be signed.
let b_crlf = true;
let sb = chilkat::StringBuilder::new();
// Use CRLF line endings.
let _ = sb.append_line("500|1|3621|4199800|400||", b_crlf);
let _ = sb.append_line("601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||", b_crlf);
let _ = sb.append_line("507|4199800|IM|2006-7888">", b_crlf);
let _ = sb.append_line("507|4199800|MS|2">", b_crlf);
// Generate the MD5 hash of what we have so far..
let md5_base64 = sb.get_hash("md5", "base64", "utf-8").unwrap_or_default();
println!("MD5 hash = {}", md5_base64);
// Complete the original file.
// After signing, we'll update the BASE64_SIGNATURE and CERT_SERIAL
let _ = sb.append_line("800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL|", b_crlf);
let _ = sb.append_line("801|M3621037.222|1|5|011|", b_crlf);
// We're going to sign the MD5 hash using the private key.
let priv_key = chilkat::PrivateKey::new();
if priv_key.load_any_format_file("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.key", "12345678a").is_err() {
println!("{}", priv_key.last_error_text());
return;
}
// Generate the ASN.1 to be signed.
// <sequence>
// <sequence>
// <oid>1.2.840.113549.2.5</oid>
// <null/>
// </sequence>
// <octets>SwxHfaJhG+N3pPqay6UzVA==</octets>
// </sequence>
let xml = chilkat::Xml::new();
xml.set_tag("sequence");
xml.update_child_content("sequence|oid", "1.2.840.113549.2.5");
xml.update_child_content("sequence|null", "");
xml.update_child_content("octets", &md5_base64);
let asn = chilkat::Asn::new();
let _ = asn.load_asn_xml(&xml.get_xml().unwrap_or_default());
println!("ASN.1 = {}", asn.get_encoded_der("base64").unwrap_or_default());
// Sign with the private key.
let rsa = chilkat::Rsa::new();
if rsa.use_private_key(&priv_key).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// Create the opaque signature.
let bd_sig = chilkat::BinData::new();
let _ = bd_sig.append_encoded(&asn.get_encoded_der("base64").unwrap_or_default(), "base64");
if rsa.sign_raw_bd(&bd_sig).is_err() {
println!("{}", rsa.last_error_text());
return;
}
// bd now contains the opaque signature, which embeds the ASN.1, which contains the MD5 hash.
// We're going to add this line:
// 800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL_NUM|
let cert = chilkat::Cert::new();
if cert.load_from_file("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.cer").is_err() {
println!("{}", cert.last_error_text());
return;
}
let serial_hex = cert.serial_number();
// The serial in hex form looks like this: 3330303031303030303030353030303033343136
// Decode to us-ascii.
let sb_serial = chilkat::StringBuilder::new();
let _ = sb_serial.decode_and_append(&serial_hex, "hex", "us-ascii");
println!("serial number in us-ascii: {}", sb_serial.get_as_string().unwrap_or_default());
let mut num_replaced = sb.replace("CERT_SERIAL", &sb_serial.get_as_string().unwrap_or_default());
num_replaced = sb.replace("BASE64_SIGNATURE", &bd_sig.get_encoded("base64").unwrap_or_default());
println!("------------------------------------");
println!("Result:");
println!("{}", sb.get_as_string().unwrap_or_default());