Examples
Every example is a complete script. Copy it into your project, or press Run to execute it in your browser with the current version of asn1js.
Basics
- Decode BER and DER: turn bytes into ASN.1 objects and handle decoding errors.
- Encode to DER: build structures and get their bytes or hex.
- Convert to JavaScript values: integers, BigInt, OIDs, strings, and dates.
- Explicit and implicit tags: encode and decode context-specific tags such as
[0].
Schemas
- Validate against a schema: check structure and read fields by name.
- CHOICE and SEQUENCE OF: alternatives and lists with
ChoiceandRepeated. - Structures inside an OCTET STRING: validate encoded data nested in another value.
Real-world data
- Read an X.509 certificate: serial number, issuer, subject, and validity from a PEM file.
- Read a public key: algorithm, curve, and key bytes from a
SubjectPublicKeyInfo. - Parse untrusted input: limit nesting, node count, and content length.