Socket
Socket
Sign inDemoInstall

asn1

Package Overview
Dependencies
1
Maintainers
12
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

asn1

Contains parsers and serializers for ASN.1 (currently BER only)

    0.2.6latest
    GitHub
    npm

Version published
Maintainers
12
Weekly downloads
18,363,059
increased by2.73%

Weekly downloads

Package description

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

Readme

Source

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(Buffer.from([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/joyent/node-asn1/issues.

FAQs

Last updated on 04 Nov 2021

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc