New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

asn1tools-js

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

asn1tools-js

ASN.1 encoding and decoding library for TypeScript/JavaScript, compatible with Python asn1tools

latest
Source
npmnpm
Version
0.1.10
Version published
Maintainers
1
Created
Source

ASN.1 Tools for TypeScript/JavaScript

A TypeScript implementation of ASN.1 (Abstract Syntax Notation One) encoding and decoding library, designed to be compatible with the Python asn1tools library. This library provides comprehensive support for message encoding/decoding in distributed systems and network protocols.

Features

  • Full TypeScript Support: Written entirely in TypeScript with complete type definitions
  • BER Encoding: Implements Basic Encoding Rules (BER) for ASN.1 encoding/decoding
  • OER Encoding: Implements Octet Encoding Rules (OER) for compact, fixed-layout encoding
  • Python Compatibility: API designed to match Python asn1tools for easy migration
  • High Performance: Optimized for high-throughput message processing
  • Comprehensive Testing: Extensive test coverage for reliability

Installation

npm install asn1tools-js

Quick Start

Basic Usage (BER — default)

import { compileString } from 'asn1tools-js';

const schema = `
  Messages DEFINITIONS ::= BEGIN
    DataRequest ::= SEQUENCE {
      messageId INTEGER,
      version INTEGER,
      category INTEGER,
      size INTEGER
    }
  END
`;

const spec = compileString(schema);

const encoded = spec.encode('DataRequest', {
  messageId: 123,
  version: 1,
  category: 2,
  size: 500
});

const decoded = spec.decode('DataRequest', encoded);

OER Encoding

OER produces a more compact, fixed-layout wire format. Pass { codec: 'oer' } when compiling:

import { compileString } from 'asn1tools-js';

const schema = `
  Messages DEFINITIONS ::= BEGIN
    BYTE  ::= INTEGER (-128..127)
    SHORT ::= INTEGER (-32768..32767)
    INT   ::= INTEGER (-2147483648..2147483647)
    LONG  ::= INTEGER (-9223372036854775808..9223372036854775807)

    SimpleMessage ::= SEQUENCE {
      id   LONG,
      flag BOOLEAN,
      data OCTET STRING (SIZE(20))
    }
  END
`;

const spec = compileString(schema, { codec: 'oer' });

const encoded = spec.encode('SimpleMessage', {
  id: 42n,
  flag: true,
  data: new Uint8Array(20).fill(0xAB)
});

const decoded = spec.decode('SimpleMessage', encoded);

Key differences from BER:

FeatureBEROER
Encoding layoutTag-Length-Value (TLV)Fixed-width, no tag/length for known-size types
Integer encodingVariable-length, minimal octetsFixed-width based on range constraint
BOOLEAN3 bytes (tag + length + value)1 byte (0x00 / 0x01)
SEQUENCE wrapperTag + length + membersMembers concatenated directly
SEQUENCE OF countWrapped in SEQUENCE TLVOER length-determinant prefix
CHOICE tagBER context-specific tag1-byte index
Single-pass encodeNo (requires length pre-calculation)Yes

OER Wire Format

ASN.1 TypeOER Encoding
BOOLEAN1 byte (0x00 = false, 0x01 = true)
INTEGER with range constraintFixed-width (1/2/4/8 bytes) based on range
OCTET STRING (SIZE(N))N bytes, raw
SEQUENCEConcatenation of member encodings
SEQUENCE OFLength-determinant count + concatenated elements
CHOICE1-byte tag index + selected alternative

The OER length-determinant for SEQUENCE OF counts:

  • 0-127: 1 byte (MSB=0, value in low 7 bits)
  • 128+: first byte MSB=1, low 7 bits = number of following octets; then value in big-endian

Working with Binary Data

import { hexToBytes, bytesToHex } from 'asn1tools-js';

const identifier = hexToBytes('973539beb5008a29ca866b178ce99f2782b5e39a');
const hexString = bytesToHex(identifier);

Supported ASN.1 Types

ASN.1 TypeTypeScript MappingDescription
INTEGERnumber | bigintSigned integers with automatic bigint handling
BOOLEANbooleanTrue/false values
OCTET STRINGUint8ArrayBinary data, identifiers, checksums
NULLnullNull values
ENUMERATEDstringNamed enumeration values
SEQUENCEobjectStructured data objects
SEQUENCE OFArray<T>Arrays of elements
CHOICEobjectUnion types with single active member

API Reference

compileFiles(filenames: string[], options?: CompileOptions): Specification

Compiles ASN.1 files into a specification object.

const spec = compileFiles(['messages.asn'], { codec: 'oer' });

compileString(content: string, options?: CompileOptions): Specification

Compiles ASN.1 content from a string.

const spec = compileString(asnContent);             // BER (default)
const spec = compileString(asnContent, { codec: 'oer' }); // OER

CompileOptions

OptionTypeDefaultDescription
codec'ber' | 'oer''ber'Encoding rules to use

Specification Methods

  • encode(typeName: string, value: any): Uint8Array — Encode a value using the specified type
  • decode(typeName: string, data: Uint8Array): any — Decode binary data using the specified type
  • getTypeNames(): string[] — Returns all available type names
  • getModuleNames(): string[] — Returns all available module names

Testing

npm test

The library includes comprehensive tests for both BER and OER:

  • Unit Tests: Individual type encoding/decoding
  • OER Tests: Primitives, SEQUENCE, SEQUENCE OF (short and long-form counts), CHOICE
  • Integration Tests: Full message compilation and processing
  • Cross-language Vectors: Hex test vectors for byte-level compatibility with other implementations

License

This project is licensed under the MIT License - see the LICENSE file for details.

  • asn1tools - Python ASN.1 library (inspiration for this project)

Keywords

asn1

FAQs

Package last updated on 15 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts