What is asn1?
The asn1 npm package is a library for encoding and decoding Abstract Syntax Notation One (ASN.1) data structures. ASN.1 is a standard interface description language for defining data structures that can be serialized and deserialized in a cross-platform way.
What are asn1's main functionalities?
BER Encoding
This feature allows you to encode data structures using Basic Encoding Rules (BER), which is one of the encoding rules provided by ASN.1.
const asn1 = require('asn1');
const Ber = asn1.Ber;
const writer = new Ber.Writer();
writer.writeInt(123);
const buffer = writer.buffer;
BER Decoding
This feature allows you to decode data structures that were encoded using BER. You can read various types of data, such as integers, from the buffer.
const asn1 = require('asn1');
const Ber = asn1.Ber;
const reader = new Ber.Reader(buffer);
const number = reader.readInt();
Defining ASN.1 Structures
This feature allows you to define custom ASN.1 structures using a fluent API and then encode them. The example shows how to define a structure with an integer and an octet string.
const asn1 = require('asn1');
const ASN1 = asn1.define('MyStructure', function() {
this.seq().obj(
this.key('id').int(),
this.key('value').octstr()
);
});
const encoded = ASN1.encode({ id: 1, value: Buffer.from('hello') }, 'der');
Other packages similar to asn1
asn1.js
asn1.js is a similar package that provides a comprehensive set of tools for ASN.1 parsing and serialization in JavaScript. It allows for both DER and BER encoding and is often used in cryptographic operations. It is similar to asn1 but may offer a different API and additional features for handling ASN.1 data structures.
node-forge
node-forge is a JavaScript library that includes various cryptography tools, including ASN.1 parsing and serialization. While it offers a broader range of cryptographic functions, its ASN.1 capabilities are similar to those of the asn1 package. It differs in that it is part of a larger suite of cryptographic tools rather than being focused solely on ASN.1.
pkijs
pkijs is a pure JavaScript library that provides the means to work with the Public Key Infrastructure (PKI) built on Web Cryptography API. It includes ASN.1 parsing and serialization as part of its feature set. It is more specialized for PKI operations compared to asn1, which is a general-purpose ASN.1 library.
node-asn1 is a library for encoding and decoding ASN.1 datatypes in pure JS.
Currently BER encoding is supported; at some point I'll likely have to do DER.
Usage
Mostly, if you're actually needing to read and write ASN.1, you probably don't
need this readme to explain what and why. If you have no idea what ASN.1 is,
see this: ftp://ftp.rsa.com/pub/pkcs/ascii/layman.asc
The source is pretty much self-explanatory, and has read/write methods for the
common types out there.
Decoding
The following reads an ASN.1 sequence with a boolean.
var Ber = require('asn1').Ber;
var reader = new Ber.Reader(new Buffer([0x30, 0x03, 0x01, 0x01, 0xff]));
reader.readSequence();
console.log('Sequence len: ' + reader.length);
if (reader.peek() === Ber.Boolean)
console.log(reader.readBoolean());
Encoding
The following generates the same payload as above.
var Ber = require('asn1').Ber;
var writer = new Ber.Writer();
writer.startSequence();
writer.writeBoolean(true);
writer.endSequence();
console.log(writer.buffer);
Installation
npm install asn1
License
MIT.
Bugs
See https://github.com/mcavage/node-asn1/issues.