-
Notifications
You must be signed in to change notification settings - Fork 0
Core API
The Core API lives in app/static/pkistudio-core.js. It is available from the npm package entry point and from @pkistudio/pkistudiojs/core.
const pkistudio = require('@pkistudio/pkistudiojs');
const document = pkistudio.parseInput(
new Uint8Array([0x30, 0x03, 0x02, 0x01, 0x01])
);
console.log(pkistudio.serializeTree(document.nodes));The same file can be loaded directly in a browser. It exposes window.PkiStudioCore.
<script src="pkistudio-core.js"></script>
<script>
const summary = window.PkiStudioCore.parseAsn1('3003020101');
console.log(summary.nodes[0].tagName);
</script>Parses DER/BER bytes, PEM text, headerless base64 text, or HEX text.
Returns:
-
format: detected or requested input format. -
bytes: decoded input bytes. -
encodedBytes: re-encoded bytes. -
nodes: parsed ASN.1 node tree.
By default, parsing validates that re-encoding matches the decoded bytes. Pass validateRoundTrip: false to skip that check.
Returns a JSON-friendly parsed summary:
formatlengthnodes
This is useful when callers do not need the internal node objects.
Use options.format when input should not be auto-detected.
Supported values:
autoderberpembase64headerless-pemhex
Converts a parsed node tree into plain JavaScript objects.
Serializes a single parsed node.
Serialization options include:
-
maxDepth: limit serialized child depth. -
includeRawValue: include raw value bytes as lowercase hex. -
includeHexPreview: include compact hex previews for primitive values. -
hexPreviewLength: control preview length. -
oidNames: object keyed by dotted OID strings. -
oidResolver: resolver object with aresolve(oid)method.
Serialized node fields include tag name, tag class, tag number, constructed flag, byte offsets, length metadata, indefinite-length status, encapsulation status, decoded value, optional OID name, optional hex preview, and children.
-
encodeNodes(nodes): re-encode a node list. -
encodeNode(node): re-encode a single parsed node. -
getNodeBytes(nodes, nodeId): re-encode a parsed node and its subtree by node ID.
The Core API also exports lower-level helpers:
decodePem(text)hexToBytes(text)base64ToBytes(base64)bytesToBase64(bytes)decodeOid(bytes)encodeOid(text)getTagName(node)describeValue(node)findNodeById(nodes, id)flattenNodes(nodes)resolveOid(oid, oidNamesOrResolver)
It also exports constants and utilities such as VERSION, CLASS_NAMES, UNIVERSAL_TAGS, concatBytes, bytesEqual, decodeInput, parseElements, getNodeValueBytes, isPemText, and toLowerHexString.
const core = require('@pkistudio/pkistudiojs/core');
const document = core.parseInput('3003020101');
const root = document.nodes[0];
console.log(document.format); // HEX
console.log(core.getTagName(root)); // SEQUENCE
console.log(core.serializeNode(root));
console.log(Array.from(core.getNodeBytes(document.nodes, root.id)));