Sample code for 30+ languages & platforms
Rust

Convert ASN.1 to/from Binary DER, XML, and Base64

Demonstrates how to convert ASN.1 from and to any of the following formats: binary DER, Base64, and XML.

Chilkat Rust Downloads

Rust

let asn = chilkat::Asn::new();

// Begin with loading ASN.1 from a binary DER/BER format file.
if asn.load_binary_file("/Users/chilkat/testData/p7b/test.p7b").is_err() {
    println!("{}", asn.last_error_text());
    return;
}

// Convert ASN.1 to XML:
let Ok(str_xml) = asn.asn_to_xml() else {
    println!("{}", asn.last_error_text());
    return;
};

// The XML returned by AsnToXml will be compact and not pretty-formatted.
// Use Chilkat XML to format the XML better:
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&str_xml).is_ok();
// Assuming success for this example..
// This is formatted better for human viewing:
println!("{}", xml.get_xml().unwrap_or_default());

// Now get the ASN.1 in base64 format.  Any encoding supported
// by Chilkat can be passed, such as "hex", "uu", "quoted-printable", "base32", "modbase64", etc.
let str_base64 = asn.get_encoded_der("base64").unwrap_or_default();

// Load the ASN.1 from XML:
let asn2 = chilkat::Asn::new();
if asn2.load_asn_xml(&xml.get_xml().unwrap_or_default()).is_err() {
    println!("{}", asn2.last_error_text());
    return;
}

// Load the ASN.1 from an encoded string, such as base64:
let asn3 = chilkat::Asn::new();
if asn3.load_encoded(&str_base64, "base64").is_err() {
    println!("{}", asn3.last_error_text());
    return;
}