npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jlguenego/asn.1

v0.0.8

Published

ASN.1 node tools

Downloads

2,128

Readme

ASN.1

A complete ASN1 tool set for Node.

  • ASN.1 BER/CER/DER message parser/generator.
  • ASN.1 Module file compiler
  • ASN.1 message validation

Requirement: node environment. Does not work on browser.

Install

Local

Using the Javascript/Typescript API.

npm i @jlguenego/asn.1

Global

Playing with the command line asn1-parse

npm i -g @jlguenego/asn.1

Usage

Javascript

// const {ASN1} = require('@jlguenego/asn1');
const {ASN1} = require('../../build/src');
const {inspect} = require('util');
const {EncodingRule} = require('../../build/src/EncodingRule');

const derMessage = `
30 # Sequence constructed (WelcomeMsg)
  1D # Length (29)
    02 # Type Integer (id)
      01 # Length 1
        12 # 0x12 = 18
    30 # Sequence constructed (someone)
      18 # Length (24)
        1B # Type GeneralString (lastname)
          07 # Length (7)
            47 55 45 4E 45 47 4F # value: GUENEGO
        1B # Type GeneralString (firstname)
          0A # Length (10)
            4A 65 61 6E 2D 4C 6F 75 69 73 # value: Jean-Louis
        01 # Type BOOLEAN (likeCoding)
          01 # Length (1)
            FF # TRUE
`;

// read from an ASN1 BER/CER/DER/JER/XER file raw content.
const message = ASN1.parseMsg(derMessage, {
  format: 'hex',
  encodingRule: EncodingRule.DER,
});
// message is a JS object with ASN1 readable/understandable content
console.log('message: ', inspect(message, false, null, true));

// ASN1 module definition parser.
const asn1ModuleStr = `
HelloProtocol DEFINITIONS ::= BEGIN

    Person ::= SEQUENCE {
        lastname           GeneralString,
        firstname          GeneralString,
        likeCoding     BOOLEAN
    }

    WelcomeMsg ::= SEQUENCE {
        id        INTEGER,
        someone   Person
    }

END
`;

const asn1Module = ASN1.getModuleFromStr(asn1ModuleStr);
const validatedMsg = ASN1.validate(asn1Module, message, 'WelcomeMsg');

// validatedMsg is the same as message, but enriched with tagged names and types.
console.log('validatedMsg: ', inspect(validatedMsg, false, null, true));

const data = {
  id: 18,
  someone: {
    lastname: 'GUENEGO',
    firstname: 'Suzana',
    likeCoding: false,
  },
};

// regenerate the original DER message.
const derBuffer = ASN1.generate(asn1Module, 'WelcomeMsg', data, {
  encodingRule: EncodingRule.DER,
});
console.log('derMessage: ', derBuffer.toString('hex'));

Command line

asn1-parse --help
Usage: asn1-parse [options] <msgFile>

Parse an ASN1 message

Arguments:
  msgFile                       file containing a ASN.1 message to parse

Options:
  -V, --version                 output the version number
  -d, --definition <asn1-file>  specify an ASN.1 definition file (.asn1)
  -t, --type <asn1-type>        specify an ASN.1 type. Must be specified with definition file.
  -f, --format <type>           specify the message format ('binary'|'hex'|'base64') (default: 'hex')
  -h, --help                    display help for command

Example

Inspired by the ASN.1 wikipedia article.

Create a protocol definition file called foo-protocol.asn1 with the ASN.1 syntax:

FooProtocol DEFINITIONS ::= BEGIN

    FooQuestion ::= SEQUENCE {
        trackingNumber INTEGER,
        question       IA5String
    }

    FooAnswer ::= SEQUENCE {
        questionNumber INTEGER,
        answer         BOOLEAN
    }

END

Create a file message called foo-question.msg with the following content (DER format):

30 13 02 01 05 16 0e 41 6e 79 62 6f 64 79 20 74 68 65 72 65 3f

Run the following:

asn1-parse -d foo-protocol.asn1 -t FooQuestion -f hex foo-question.msg

The output should be:

{
  "tagClass": "UNIVERSAL",
  "tagDefinedType": "FooQuestion",
  "isConstructed": true,
  "tagCode": 16,
  "tagLabel": "SEQUENCE",
  "length": 19,
  "lengthType": "DEFINITE",
  "value": [
    {
      "tagClass": "UNIVERSAL",
      "tagName": "trackingNumber",
      "isConstructed": false,
      "tagCode": 2,
      "tagLabel": "INTEGER",
      "length": 1,
      "lengthType": "DEFINITE",
      "value": 5
    },
    {
      "tagClass": "UNIVERSAL",
      "tagName": "question",
      "isConstructed": false,
      "tagCode": 22,
      "tagLabel": "IA5String",
      "length": 14,
      "lengthType": "DEFINITE",
      "value": "Anybody there?"
    }
  ]
}

Typescript

This module is written in Typescript. No need to install typings.

Context

This library has been designed according the content of:

Contribute

Feel free to raise an issue if you need something.

Author

Made with :heart: by Jean-Louis GUENEGO [email protected]