Rust Requires Chilkat v11.0.0+
Rust
Aadhaar Paperless Offline e-kyc
See more XML Digital Signatures Examples
Opens an encrypted .zip containing Aadhaar Paperless Offline e-KYC XML. Gets the XML and validates the digital signature. Then computes the hash for the mobile number and Email ID.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Open the .zip containing the Aadhaar Paperless Offline e-KYC XML.
// The .zip is encrypted using the "Share Phrase".
let zip = chilkat::Zip::new();
if zip.open_zip("qa_data/xml_dsig/offline_paperless_kyc.zip").is_err() {
println!("{}", zip.last_error_text());
return;
}
// The .zip should contain 1 XML file.
let entry = chilkat::ZipEntry::new();
if zip.entry_at(0, &entry).is_err() {
println!("{}", zip.last_error_text());
return;
}
// To get the contents, we need to specify the Share Phrase.
let share_phrase = "Lock@487".to_string();
zip.set_decrypt_password(&share_phrase);
let bd_xml = chilkat::BinData::new();
// The XML file will be unzipped into the bdXml object.
if entry.unzip_to_bd(&bd_xml).is_err() {
println!("{}", entry.last_error_text());
return;
}
// First verify the XML digital signature.
let dsig = chilkat::XmlDSig::new();
if dsig.load_signature_bd(&bd_xml).is_err() {
println!("{}", dsig.last_error_text());
return;
}
// The UIDAI XML signature does not contain the KeyInfo, so we must load the uidai certificate
// and indicate that its public key is to be used for verifying the signature.
let cert = chilkat::Cert::new();
if cert.load_from_file("qa_data/xml_dsig/uidai_auth_sign_prod_2023.cer").is_err() {
println!("{}", cert.last_error_text());
return;
}
// Get the certificate's public key.
let pub_key = chilkat::PublicKey::new();
let _ = cert.get_public_key(&pub_key);
let _ = dsig.set_public_key(&pub_key);
// The XML in this example contains only 1 signature.
let b_verify_reference_digests = true;
if dsig.verify_signature(b_verify_reference_digests).is_err() {
println!("{}", dsig.last_error_text());
println!("The signature was not valid.");
return;
}
println!("The XML digital signature is valid.");
// Let's compute the hash for the Mobile Number.
// Hashing logic for Mobile Number :
// Sha256(Sha256(Mobile+SharePhrase))*number of times last digit of Aadhaar number
// (Ref ID field contains last 4 digits).
//
// Example :
// Mobile: 1234567890
// Aadhaar Number:XXXX XXXX 3632
// Passcode : Lock@487
// Hash: Sha256(Sha256(1234567890Lock@487))*2
// In case of Aadhaar number ends with Zero we will hashed one time.
let crypt = chilkat::Crypt2::new();
crypt.set_hash_algorithm("sha256");
crypt.set_encoding_mode("hexlower");
let mut str_to_hash = "1234567890Lock@487".to_string();
let bd_hash = chilkat::BinData::new();
let _ = bd_hash.append_string(&str_to_hash, "utf-8").is_ok();
// Hash a number of times equal to the last digit of your Aadhaar number.
// If the Aadhaar number ends with 0, then hash one time.
// For this example, we'll just set the number of times to hash
// for the case where an Aadhaar number ends in "9"
let num_times_to_hash = 9;
for i in 1..=num_times_to_hash {
let tmp_str = crypt.hash_bd_enc(&bd_hash).unwrap_or_default();
let _ = bd_hash.clear();
let _ = bd_hash.append_string(&tmp_str, "utf-8");
}
println!("Computed Mobile hash = {}", bd_hash.get_string("utf-8").unwrap_or_default());
// Let's get the mobile hash stored in the XML and compare it with our computed hash.
let xml = chilkat::Xml::new();
let _ = xml.load_bd(&bd_xml, true).is_ok();
let m_hash = xml.chilkat_path("UidData|Poi|(m)").unwrap_or_default();
println!("Stored Mobile hash = {}", m_hash);
// Now do the same thing for the email hash:
str_to_hash = "abc@gm.comLock@487".to_string();
let _ = bd_hash.clear();
let _ = bd_hash.append_string(&str_to_hash, "utf-8").is_ok();
for i in 1..=num_times_to_hash {
let tmp_str = crypt.hash_bd_enc(&bd_hash).unwrap_or_default();
let _ = bd_hash.clear();
let _ = bd_hash.append_string(&tmp_str, "utf-8");
}
println!("Computed Email hash = {}", bd_hash.get_string("utf-8").unwrap_or_default());
let e_hash = xml.chilkat_path("UidData|Poi|(e)").unwrap_or_default();
println!("Stored Email hash = {}", e_hash);