Skip to content

encodeName

DER-encodes an X.509 Name.

Returns a DER SEQUENCE of RelativeDistinguishedNames (RDNs).
Each RDN emitted by this helper contains exactly one name attribute.

ts
function encodeName(
	input: NameInput,
): Uint8Array

Parameters

  • input: NameInput — Name fields in convenience-object form or caller-ordered attribute form.

Returns — DER-encoded X.509 Name bytes.

Throws

  • Error — If the input produces no attributes, contains an unsupported field key, or uses an invalid country code.

See also

  • RFC 5280 Appendix A.1

    NameObject input emits populated fields in the canonical order from NAME_OBJECT_ORDER.
    NameAttribute array input preserves caller-supplied ordering, but each entry still becomes its own single-attribute RDN.

    Attribute OIDs and ASN.1 string encodings come from NAME_FIELD_DEFINITIONS.
    Empty strings and undefined fields are ignored when the input is a NameObject.

Examples

ts
const der = encodeName({ country: 'US', commonName: 'example.com' });

// emits two single-attribute RDNs: C=US, then CN=example.com
ts
const der = encodeName([
	{ type: 'country', value: 'US' },
	{ type: 'commonName', value: 'example.com' },
]);

// preserves caller order: C first, then CN

Released under the MIT License.