Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
local asn = chilkat.Asn.new()
-- Begin with loading ASN.1 from a binary DER/BER format file.
local success = asn:load_binary_file("/Users/chilkat/testData/p7b/test.p7b")
if not success then
print(asn.last_error_text)
return
end
-- Convert ASN.1 to XML:
local strXml = asn:asn_to_xml()
if not asn.last_method_success then
print(asn.last_error_text)
return
end
-- The XML returned by AsnToXml will be compact and not pretty-formatted.
-- Use Chilkat XML to format the XML better:
local xml = chilkat.Xml.new()
success = xml:load_xml(strXml)
-- Assuming success for this example..
-- This is formatted better for human viewing:
print(xml:get_xml())
-- 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.
local strBase64 = asn:get_encoded_der("base64")
-- Load the ASN.1 from XML:
local asn2 = chilkat.Asn.new()
success = asn2:load_asn_xml(xml:get_xml())
if not success then
print(asn2.last_error_text)
return
end
-- Load the ASN.1 from an encoded string, such as base64:
local asn3 = chilkat.Asn.new()
success = asn3:load_encoded(strBase64, "base64")
if not success then
print(asn3.last_error_text)
return
end