Skip to content

micro509/der ​

DER reading and writing primitives.

The building blocks every other entrypoint is made of, for the cases the typed APIs do not cover:

Reading accepts definite lengths and minimal length encodings, rejects the high-tag-number form, and applies a nesting-depth guard capped at DEFAULT_MAX_DER_DEPTH. BER-only constructs fail.

Readers and decoders take untrusted bytes, so each one comes as a pair:

Writers take typed input and throw, matching encodeName and pemEncode.

All operations are synchronous.

Functions ​

  • decodeDerBitString — Decodes a BIT STRING element (tag 0x03) into a DerBitString.
  • decodeDerBitStringOrThrow — Decodes a BIT STRING element (tag 0x03) into a DerBitString.
  • decodeDerBoolean — Decodes a BOOLEAN element (tag 0x01).
  • decodeDerBooleanOrThrow — Decodes a BOOLEAN element (tag 0x01).
  • decodeDerInteger — Decodes an INTEGER element (tag 0x02) into a number.
  • decodeDerIntegerOrThrow — Decodes an INTEGER element (tag 0x02) into a number.
  • decodeDerOctetString — Decodes an OCTET STRING element (tag 0x04) into its payload bytes.
  • decodeDerOctetStringOrThrow — Decodes an OCTET STRING element (tag 0x04) into its payload bytes.
  • decodeDerOid — Decodes an OBJECT IDENTIFIER element (tag 0x06) into dotted-decimal form.
  • decodeDerOidOrThrow — Decodes an OBJECT IDENTIFIER element (tag 0x06) into dotted-decimal form.
  • decodeDerString — Decodes a string element into text, dispatching on its tag.
  • decodeDerStringOrThrow — Decodes a string element into text, dispatching on its tag.
  • decodeDerTime — Decodes a UTCTime (tag 0x17) or GeneralizedTime (tag 0x18) element into a Date.
  • decodeDerTimeOrThrow — Decodes a UTCTime (tag 0x17) or GeneralizedTime (tag 0x18) element into a Date.
  • derChildren — Reads the direct children of a constructed parent within source.
  • derChildrenOrThrow — Reads the direct children of a constructed parent within source.
  • readDerElement — Reads one TLV element from bytes starting at offset.
  • readDerElementOrThrow — Reads one TLV element from bytes starting at offset.
  • readDerRoot — Reads the single top-level TLV element from bytes.
  • readDerRootOrThrow — Reads the single top-level TLV element from bytes.
  • readDerSequence — Reads a DER-encoded SEQUENCE from bytes and returns its direct children.
  • readDerSequenceOrThrow — Reads a DER-encoded SEQUENCE from bytes and returns its direct children.
  • hexToBytes — Converts a hex string (even or odd length) to a Uint8Array.
  • toHex — Converts raw bytes to a lowercase hex string with no separator.
  • assertDerMaxDepth — Walks the full DER tree rooted in bytes.
  • derBitString — Encodes a DER BIT STRING (tag 0x03).
  • derBmpString — Encodes a DER BMPString (tag 0x1e) as big-endian UTF-16.
  • derBoolean — Encodes a DER BOOLEAN (tag 0x01): true → 0xff, false → 0x00.
  • concatBytes — Concatenates multiple byte arrays into a single Uint8Array.
  • derExplicitContext — Wraps a value in an explicit context-specific constructed tag (0xa0 + tag).
  • derGeneralizedTime — Encodes a Date as a DER GeneralizedTime (tag 0x18), format YYYYMMDDHHMMSSZ.
  • derIa5String — Encodes a DER IA5String (tag 0x16).
  • derImplicitConstructedContext — Wraps a value in an implicit context-specific constructed tag (0xa0 + tag).
  • derImplicitPrimitiveContext — Wraps a value in an implicit context-specific primitive tag (0x80 + tag).
  • derInteger — Encodes raw big-endian bytes as a DER INTEGER (tag 0x02).
  • derIntegerFromNumber — Encodes a non-negative JavaScript number as a DER INTEGER.
  • derNull — Produces a DER NULL element (tag 0x05, zero-length value).
  • derOid — Encodes a dotted-decimal OID string as a DER OBJECT IDENTIFIER (tag 0x06).
  • derOctetString — Wraps raw bytes in an OCTET STRING element (tag 0x04).
  • derPrintableString — Encodes a DER PrintableString (tag 0x13).
  • derSequence — Wraps concatenated children in a SEQUENCE (tag 0x30).
  • derSet — Wraps children in a SET (tag 0x31) after DER-sorting them lexicographically by encoded bytes,
  • derTime — Encodes a Date as the appropriate DER time type per RFC 5280.
  • derTlv — Builds a complete DER TLV (tag-length-value) element:
  • derUniversalString — Encodes a DER UniversalString (tag 0x1c) as big-endian UTF-32.
  • derUtcTime — Encodes a Date as a DER UTCTime (tag 0x17), format YYMMDDHHMMSSZ.
  • derUtf8String — Encodes a DER UTF8String (tag 0x0c).

Types ​

Variables ​

Released under the MIT License.