Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { Asn, Xml } from '@chilkat/react-native'

function chilkatExample() {
  const asn = new Asn();

  // Begin with loading ASN.1 from a binary DER/BER format file.
  try {
    asn.loadBinaryFile('/Users/chilkat/testData/p7b/test.p7b');
  } catch {
    console.log(asn.lastErrorText);
    return;
  }

  // Convert ASN.1 to XML:
  let strXml: string;
  try {
    strXml = asn.asnToXml();
  } catch {
    console.log(asn.lastErrorText);
    return;
  }

  // The XML returned by AsnToXml will be compact and not pretty-formatted.
  // Use Chilkat XML to format the XML better:
  const xml = new Xml();
  xml.loadXml(strXml);
  // Assuming success for this example..
  // This is formatted better for human viewing:
  console.log(xml.getXml());

  // 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.
  const strBase64 = asn.getEncodedDer('base64');

  // Load the ASN.1 from XML:
  const asn2 = new Asn();
  try {
    asn2.loadAsnXml(xml.getXml());
  } catch {
    console.log(asn2.lastErrorText);
    return;
  }

  // Load the ASN.1 from an encoded string, such as base64:
  const asn3 = new Asn();
  try {
    asn3.loadEncoded(strBase64, 'base64');
  } catch {
    console.log(asn3.lastErrorText);
    return;
  }
}