1asn1js
2======
3
4asn1js is a JavaScript generic ASN.1 parser/decoder that can decode any valid ASN.1 DER or BER structures.
5
6An example page that can decode Base64-encoded (raw base64, PEM armoring and `begin-base64` are recognized) or Hex-encoded (or local files with some browsers) is included and can be used both [online on the official website](https://asn1js.eu/) or [offline (ZIP file)](https://lapo.it/asn1js/asn1js.zip) by opening `index-local.html`.
7
8Usage with `nodejs`
9-------------------
10
11This package can be installed with either npm or yarn via the following commands:
12
13```sh
14npm install @lapo/asn1js
15# or other tools
16pnpm install @lapo/asn1js
17yarn add @lapo/asn1js
18```
19
20You can import the classes like this:
21
22```js
23import { ASN1 } from '@lapo/asn1js';
24```
25
26A submodule of this package can also be imported:
27
28```js
29import { Hex } from '@lapo/asn1js/hex.js';
30```
31
32If your code is still not using ES6 Modules (and is using CommonJS) you can `require` it normally [since NodeJS 22](https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/) (with parameter `--experimental-require-module`):
33
34```js
35const
36 { ASN1 } = require('@lapo/asn1js'),
37 { Hex } = require('@lapo/asn1js/hex.js');
38console.log(ASN1.decode(Hex.decode('06032B6570')).content());
39```
40
41On older NodeJS you instead need to use async `import`:
42
43```js
44async function main() {
45 const
46 { ASN1 } = await import('@lapo/asn1js'),
47 { Hex } = await import('@lapo/asn1js/hex.js');
48 console.log(ASN1.decode(Hex.decode('06032B6570')).content());
49}
50main();
51```
52
53Usage on the web
54--------------------
55
56Can be [tested on JSFiddle](https://jsfiddle.net/lapo/y6t2wo7q/).
57
58```html
59<script>
60import { ASN1 } from 'https://unpkg.com/@lapo/asn1js@2.0.0/asn1.js';
61import { Hex } from 'https://unpkg.com/@lapo/asn1js@2.0.0/hex.js';
62
63document.body.innerText = ASN1.decode(Hex.decode('06032B6570')).content();
64</script>
65```
66
67Local usage
68--------------------
69
70Since unfortunately ESM modules are not working on `file:` protocol due to [CORS issues](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#other_differences_between_modules_and_standard_scripts), there is a bundled [single-file version working locally](https://asn1js.eu/index-local.html). It doesn't work online (due to [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) restrictions about inline content) but can be saved locally and opened in a browser.
71
72Usage from CLI
73--------------------
74
75You can dump an ASN.1 structure from the command line using the following command (no need to even install it):
76
77```sh
78npx @lapo/asn1js ed25519.cer
79```
80
81ISC license
82-----------
83
84ASN.1 JavaScript decoder Copyright (c) 2008-2025 Lapo Luchini <lapo@lapo.it>
85
86Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
87
88THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
89
90credits
91-------
92
93- OBJECT IDENTIFIER values are recognized using data taken from Peter Gutmann's [dumpasn1](https://www.cs.auckland.ac.nz/~pgut001/#standards) program.
94- BMPString support added by [Felipe Gasper](https://github.com/FGasper)
95- extended tag support added by [Péter Budai](https://www.peterbudai.eu/)
96- patches by [Gergely Nagy](https://github.com/ngg)
97- Relative OID support added by [Mistial Developer](https://github.com/mistial-dev)
98- dark mode and other UI improvements by [Oliver Burgmaier](https://github.com/olibu/)
99- patches by [Nicolai Søborg](https://github.com/NicolaiSoeborg)
100
101links
102-----
103
104- [official website](https://lapo.it/asn1js/)
105- [dedicated domain](https://asn1js.eu/)
106- [InDefero tracker](http://idf.lapo.it/p/asn1js/)
107- [GitHub mirror](https://github.com/lapo-luchini/asn1js)
108- [Ohloh code stats](https://www.openhub.net/p/asn1js)