What is @ldapjs/asn1?
@ldapjs/asn1 is a Node.js 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. This package is particularly useful for working with LDAP (Lightweight Directory Access Protocol) data, which often uses ASN.1 encoding.
What are @ldapjs/asn1's main functionalities?
Encoding ASN.1 Data
This feature allows you to encode data into ASN.1 format. The code sample demonstrates how to create a BER (Basic Encoding Rules) writer, start a sequence, write an integer, and then end the sequence. The resulting buffer contains the encoded ASN.1 data.
const asn1 = require('@ldapjs/asn1');
const Ber = asn1.Ber;
const writer = new Ber.Writer();
writer.startSequence();
writer.writeInt(42);
writer.endSequence();
const encodedData = writer.buffer;
console.log(encodedData);
Decoding ASN.1 Data
This feature allows you to decode ASN.1 data. The code sample demonstrates how to create a BER reader, read a sequence, and then read an integer from the encoded data. The resulting value is the decoded integer.
const asn1 = require('@ldapjs/asn1');
const Ber = asn1.Ber;
const encodedData = Buffer.from([0x30, 0x03, 0x02, 0x01, 0x2A]);
const reader = new Ber.Reader(encodedData);
reader.readSequence();
const value = reader.readInt();
console.log(value); // 42
Other packages similar to @ldapjs/asn1
asn1.js
asn1.js is a pure JavaScript implementation of ASN.1. It provides a comprehensive set of tools for encoding and decoding ASN.1 data structures. Compared to @ldapjs/asn1, asn1.js offers more flexibility and is widely used in various cryptographic applications.
@ldapjs/asn1
@ldapjs/asn1
is a library for encoding and decoding ASN.1 datatypes in pure
JS. Currently BER encoding is supported.
Decoding
The following reads an ASN.1 sequence with a boolean.
const { BerReader, BerTypes } = require('@ldapjs/asn1')
const reader = new BerReader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]))
reader.readSequence()
console.log('Sequence len: ' + reader.length)
if (reader.peek() === BerTypes.Boolean)
console.log(reader.readBoolean())
Encoding
The following generates the same payload as above.
const { BerWriter } = require('@ldapjs/asn1');
const writer = new BerWriter();
writer.startSequence();
writer.writeBoolean(true);
writer.endSequence();
console.log(writer.buffer);
Installation
npm install @ldapjs/asn1
Bugs
See https://github.com/ldapjs/asn1/issues.