public final class Der extends Object
Why hand-written: NetPhantom is Java 8 and dependency-free by policy. The JDK has no public API for BUILDING certificates - only for parsing them - and the internal sun.security.x509 classes are absent from the compile-time API of the JDK 17 and JDK 21 cross-compilation passes, so using them would break the three-pass build. Encoding DER directly needs only java.security for the cryptography, and the JDK's own CertificateFactory then parses and verifies the result, which keeps the encoder honest: everything produced here is round-tripped through an independent implementation in the test harness.
Only the universal types an X.509 certificate needs are provided: SEQUENCE, SET, INTEGER, BOOLEAN, OBJECT IDENTIFIER, UTF8String, IA5String, PrintableString, BIT STRING, OCTET STRING, UTCTime, GeneralizedTime and context-specific tagging. All methods are static and thread-safe; instances are the growing byte buffers themselves.
| Modifier and Type | Field and Description |
|---|---|
static int | BIT_STRINGUniversal tag: BIT STRING. |
static int | BOOLEANUniversal tag: BOOLEAN. |
static int | GENERALIZED_TIMEUniversal tag: GeneralizedTime. |
static int | IA5_STRINGUniversal tag: IA5String. |
static int | INTEGERUniversal tag: INTEGER. |
static int | OCTET_STRINGUniversal tag: OCTET STRING. |
static int | OIDUniversal tag: OBJECT IDENTIFIER. |
static int | PRINTABLE_STRINGUniversal tag: PrintableString. |
static int | SEQUENCEUniversal tag: SEQUENCE (constructed). |
static int | SETUniversal tag: SET (constructed). |
static int | UTC_TIMEUniversal tag: UTCTime. |
static int | UTF8_STRINGUniversal tag: UTF8String. |
| Modifier and Type | Method and Description |
|---|---|
static byte[] | bitString(byte[] bits)Encodes a BIT STRING with zero unused bits, which is what signatures and public keys use. |
static byte[] | bool(boolean value)Encodes a BOOLEAN. |
static byte[] | constructed(int tag, byte[]... parts)Encodes a constructed value (SEQUENCE, SET, or an explicit context tag) from parts. |
static byte[] | contextExplicit(int n, byte[] inner)Encodes an EXPLICIT context-specific tag [n], wrapping one inner encoding. |
static byte[] | contextImplicit(int n, byte[] content)Encodes an IMPLICIT context-specific PRIMITIVE tag [n] over raw content bytes. |
static byte[] | ia5(String s)Encodes an IA5String (ASCII). |
static byte[] | integer(BigInteger value)Encodes an INTEGER from a BigInteger. |
static byte[] | integer(int value)Encodes a small non-negative INTEGER. |
static byte[] | namedBits(int bitsHighFirst, int bitCount)Encodes a BIT STRING for X.509 KeyUsage: named bits, DER minimal-length rules applied (trailing zero bits stripped, unused-bit count set accordingly). |
static byte[] | octetString(byte[] value)Encodes an OCTET STRING. |
static byte[] | oid(String dotted)Encodes an OBJECT IDENTIFIER from dotted-decimal text, e.g. |
static byte[] | sequence(byte[]... parts)Encodes a SEQUENCE. |
static byte[] | set(byte[]... parts)Encodes a SET. |
static byte[] | tlv(int tag, byte[] value)Encodes a TLV (tag, length, value) triplet. |
static byte[] | utf8(String s)Encodes a UTF8String. |
public static final int BOOLEAN
public static final int INTEGER
public static final int BIT_STRING
public static final int OCTET_STRING
public static final int OID
public static final int UTF8_STRING
public static final int PRINTABLE_STRING
public static final int IA5_STRING
public static final int UTC_TIME
public static final int GENERALIZED_TIME
public static final int SEQUENCE
public static final int SET
public static byte[] tlv(int tag,
byte[] value)tag - The DER tag byte (including class and constructed bits).value - The already-encoded content bytes.public static byte[] constructed(int tag,
byte[]... parts)tag - The constructed tag.parts - The already-encoded children, concatenated in order.public static byte[] sequence(byte[]... parts)
public static byte[] set(byte[]... parts)
public static byte[] contextExplicit(int n,
byte[] inner)n - The context tag number, 0-30.inner - The complete inner encoding.public static byte[] contextImplicit(int n,
byte[] content)n - The context tag number, 0-30.content - The raw content (not a TLV).public static byte[] integer(BigInteger value)
BigInteger.toByteArray() already produces, minimal-length included.public static byte[] integer(int value)
public static byte[] bool(boolean value)
public static byte[] oid(String dotted)
The first two arcs combine into one byte as 40*first+second; every later arc is base-128 with the continuation bit set on all but the last byte.
dotted - The OID in dotted-decimal form.IllegalArgumentException - If the text is not a valid OID.public static byte[] utf8(String s)
public static byte[] ia5(String s)
public static byte[] bitString(byte[] bits)
public static byte[] namedBits(int bitsHighFirst,
int bitCount)bitsHighFirst - Bit 0 = most significant bit of the first byte, per X.680.bitCount - How many bits are meaningful.public static byte[] octetString(byte[] value)
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.