info > Convert::ASN1

```html

📘 NAME

Convert::ASN1 — ASN.1 Encode/Decode library

🚀 Quick Reference

Use CaseCommandDescription
🔧 Instantiate$asn = Convert::ASN1->new;Create a new encoder/decoder object
📄 Prepare spec$asn->prepare($asn_string);Compile an ASN.1 description from a string or filehandle
📨 Encode$pdu = $asn->encode(%vars);Encode a Perl data structure into a PDU
📩 Decode$hash = $asn->decode($pdu);Decode a PDU into a Perl hash reference
🔍 Find macro$obj = $asn->find($macro);Select a specific macro from a multi-macro spec
📡 Socket recvasn_recv($sock, $buffer, 0);Read a single element from a socket
📡 Socket sendasn_send($sock, $buffer);Send a buffer (identical to send)
📁 File readasn_read($fh, $buffer);Read a single element from a filehandle
📁 File writeasn_write($fh, $buffer);Write a buffer (identical to syswrite)
đŸ—„ī¸ Buffered read$elem = asn_get($fh);Read a single element with buffering
✅ Check readyasn_ready($fh);Check if a complete element is buffered

📌 VERSION

version 0.33

📋 SYNOPSIS

use Convert::ASN1;

$asn = Convert::ASN1->new;
$asn->prepare(q<

  [APPLICATION 7] SEQUENCE {
    int INTEGER,
    str OCTET STRING
  }

>);

$pdu = $asn->encode( int => 7, str => "string");

$out = $asn->decode($pdu);
print $out->{int}," ",$out->{str},"\n";

use Convert::ASN1 qw(:io);

$peer   = asn_recv($sock,$buffer,0);
$nbytes = asn_read($fh, $buffer);
$nbytes = asn_send($sock, $buffer, $peer);
$nbytes = asn_send($sock, $buffer);
$nbytes = asn_write($fh, $buffer);
$buffer = asn_get($fh);
$yes    = asn_ready($fh);

📖 DESCRIPTION

Convert::ASN1 encodes and decodes ASN.1 data structures using BER/DER rules.

âš™ī¸ METHODS

🆕 new ( [OPTIONS] )

Constructor, creates a new object. If given, OPTIONS are the same ones as for configure ( OPTIONS ) below.

❌ error ()

Returns the last error.

🔧 configure ( OPTIONS )

Configure options to control how Convert::ASN1 will perform various tasks. Options are passed as name-value pairs.

đŸ”ĸ Encode options

🔓 Decode options

📄 prepare ( ASN )

Compile the given ASN.1 description which can be passed as a string or as a filehandle. The syntax used is very close to ASN.1, but has a few differences. If the ASN describes only one macro then encode/decode can be called on this object. If ASN describes more than one ASN.1 macro then find must be called. The method returns undef on error.

📁 prepare_file ( ASNPATH )

Compile the ASN.1 description to be read from the specified pathname.

🔍 find ( MACRO )

Find a macro from a prepared ASN.1 description. Returns an object which can be used for encode/decode.

📨 encode ( VARIABLES )

Encode a PDU. Top-level variables are passed as name-value pairs, or as a reference to a hash containing them. Returns the encoded PDU, or undef on error.

📩 decode ( PDU )

Decode the PDU, returns a reference to a hash containing the values for the PDU. Returns undef if there was an error.

📇 registeroid ( OID, HANDLER )

Register a handler for all ASN.1 elements that are "DEFINED BY" the given OID. HANDLER must be a Convert::ASN1 object, e.g. as returned by find ( MACRO ).

đŸˇī¸ registertype ( NAME, OID, HANDLER )

Register a handler for all ASN.1 elements named "NAME", that are "DEFINED BY" the given OID. HANDLER must be a Convert::ASN1 object, e.g. as returned by find ( MACRO ).

đŸ“Ļ EXPORTS

As well as providing an object interface for encoding/decoding PDUs, Convert::ASN1 also provides the following functions.

🔌 IO Functions

🔐 Encode/Decode Functions

đŸ”ĸ Constants

🐛 Debug Functions

đŸˇī¸ EXPORT TAGS

🔄 MAPPING ASN.1 TO PERL

Every element in the ASN.1 definition has a name; in Perl a hash is used with these names as an index and the element value as the hash value.

# ASN.1
int INTEGER,
str OCTET STRING

# Perl
{ int => 5, str => "text" }

In the case of a SEQUENCE, SET or CHOICE then the value in the namespace will be a hash reference which will be the namespace for the elements within that element.

# ASN.1
int INTEGER,
seq SEQUENCE {
  str OCTET STRING,
  bool BOOLEAN
}

# Perl
{ int => 5, seq => { str => "text", bool => 1}}

If the element is a SEQUENCE OF, or SET OF, then the value in the namespace will be an array reference. The elements in the array will be of the type expected by the type following the OF. For example with SEQUENCE OF STRING the array would contain strings. With SEQUENCE OF SEQUENCE { ... } the array will contain hash references which will be used as namespaces.

# ASN.1
int INTEGER,
str SEQUENCE OF OCTET STRING

# Perl
{ int => 5, str => [ "text1", "text2"]}

# ASN.1
int INTEGER,
str SEQUENCE OF SEQUENCE {
  type OCTET STRING,
  value INTEGER
}

# Perl
{ int => 5, str => [
  { type => "abc", value => 4 },
  { type => "def", value => -1 },
]}

Finally, if you wish to pre-parse ASN.1 and hold it to include inline in your PDU, you can coerce it into the ASN.1 spec by defining the value as ANY in the schema, and then pass the pre-encoded value inline.

# ASN.1
int INTEGER,
str OCTET STRING,
pre ANY

# Perl
{ int => 5, str => "text", pre=>"\x03\x03\x00\x0a\x05" }

This passes a pre-encoded BIT STRING instance as hex text. It could be a previous result of $obj->encode() from another run held in some variable.

âš ī¸ Exceptions

There are some exceptions where Convert::ASN1 does not require an element to be named. These are SEQUENCE {...}, SET {...} and CHOICE. In each case if the element is not given a name then the elements inside the {...} will share the same namespace as the elements outside of the {...}.

📝 TODO

👤 AUTHOR

Graham Barr <gbarr AT cpan.org>

🆘 SUPPORT

Report issues via github at https://github.com/gbarr/perl-Convert-ASN1/issues

To contribute, create a git fork of the repository at https://github.com/gbarr/perl-Convert-ASN1, do your work on a fresh branch created from master, and submit a pull request.

ÂŠī¸ COPYRIGHT

Copyright (c) 2000–2012 Graham Barr <gbarr AT cpan.org>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

```
Convert::ASN1
📘 NAME 🚀 Quick Reference 📌 VERSION 📋 SYNOPSIS 📖 DESCRIPTION âš™ī¸ METHODS
🆕 new ( [OPTIONS] ) ❌ error () 🔧 configure ( OPTIONS ) 📄 prepare ( ASN ) 📁 prepare_file ( ASNPATH ) 🔍 find ( MACRO ) 📨 encode ( VARIABLES ) 📩 decode ( PDU ) 📇 registeroid ( OID, HANDLER ) đŸˇī¸ registertype ( NAME, OID, HANDLER )
đŸ“Ļ EXPORTS
🔌 IO Functions 🔐 Encode/Decode Functions đŸ”ĸ Constants 🐛 Debug Functions
đŸˇī¸ EXPORT TAGS 🔄 MAPPING ASN.1 TO PERL
âš ī¸ Exceptions
📝 TODO 👤 AUTHOR 🆘 SUPPORT ÂŠī¸ COPYRIGHT

Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-14 20:30 @2600:1f28:365:80b0:4d23:66fa:c2bb:7bae
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!