Encode to DER
Build a structure from constructors, then call toBER() for the bytes or toString("hex") for a hex string. asn1js encodes the values in the order you add them and does not sort SET OF elements, so sort them yourself when you need DER.
import * as asn1js from "asn1js";
// SEQUENCE { INTEGER 42, UTF8String "hello", BOOLEAN TRUE }const sequence = new asn1js.Sequence({ value: [ new asn1js.Integer({ value: 42 }), new asn1js.Utf8String({ value: "hello" }), new asn1js.Boolean({ value: true }), ],});
// toBER() returns an ArrayBufferconst der = sequence.toBER();console.log("bytes:", der.byteLength);
// toString("hex") returns the same encoding as a hex stringconsole.log("hex:", sequence.toString("hex"));
// Structures can also be built step by stepconst list = new asn1js.Sequence();for (const value of [1, 2, 3]) { list.valueBlock.value.push(new asn1js.Integer({ value }));}console.log("list:", list.toString("hex"));API: BaseBlock.toBER(), Sequence