Package com.yubico.internal.util
Class BinaryUtil
java.lang.Object
com.yubico.internal.util.BinaryUtil
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic final classstatic interfaceFunction to parse an element of a DER SEQUENCE. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]concat(byte[]... arrays) Return a new array containing the concatenation of the argumentarrays.static byte[]copy(byte[] bytes) static byte[]copyInto(byte[] src, byte[] dest, int destFrom) Copysrcintodestbeginning at the offsetdestFrom, then return the modifieddest.static byte[]encodeDerBitStringWithZeroUnused(@lombok.NonNull byte[] content) static byte[]encodeDerLength(int length) static byte[]encodeDerObjectId(@lombok.NonNull byte[] oid) static byte[]encodeDerSequence(byte[]... items) static byte[]encodeUint16(int value) static byte[]encodeUint32(long value) static byte[]static intgetUint16(byte[] bytes) Read 2 bytes as a big endian unsigned 16-bit integer.static longgetUint32(byte[] bytes) Read 4 bytes as a big endian unsigned 32-bit integer.static shortgetUint8(byte b) Read one byte as an unsigned 8-bit integer.static BinaryUtil.ParseDerResult<Integer>parseDerLength(@lombok.NonNull byte[] der, int offset) static BinaryUtil.ParseDerResult<byte[]>parseDerOctetString(@lombok.NonNull byte[] der, int offset) Parse an Octet String.static <T> BinaryUtil.ParseDerResult<List<T>>parseDerSequence(@lombok.NonNull byte[] der, int offset, @NonNull BinaryUtil.ParseDerSequenceElementFunction<T> parseElement) Parse a SEQUENCE using the given element parsing function.static <T> BinaryUtil.ParseDerResult<List<T>>parseDerSequenceContents(@lombok.NonNull byte[] der, int offset, int endOffset, @NonNull BinaryUtil.ParseDerSequenceElementFunction<T> parseElement) Parse the elements of a SEQUENCE using the given element parsing function.static BinaryUtil.ParseDerResult<Optional<Integer>>parseDerTaggedOrSkip(@lombok.NonNull byte[] der, int offset, byte expectTag, boolean constructed, BinaryUtil.DerTagClass expectTagClass) Parse a DER header with the given tag value, constructed bit and tag class, and return the start and end offsets of the value octets.static byte[]readAll(InputStream is) static bytesingleFromHex(String hex) Parse a single byte from two hexadecimal characters.static StringtoHex(byte[] bytes)
-
Constructor Details
-
BinaryUtil
public BinaryUtil()
-
-
Method Details
-
copy
public static byte[] copy(byte[] bytes) -
copyInto
public static byte[] copyInto(byte[] src, byte[] dest, int destFrom) Copysrcintodestbeginning at the offsetdestFrom, then return the modifieddest. -
concat
public static byte[] concat(byte[]... arrays) Return a new array containing the concatenation of the argumentarrays. -
toHex
- Parameters:
bytes- Bytes to encode
-
fromHex
- Parameters:
hex- String of hexadecimal digits to decode as bytes.
-
singleFromHex
Parse a single byte from two hexadecimal characters.- Parameters:
hex- String of hexadecimal digits to decode as bytes.
-
getUint8
public static short getUint8(byte b) Read one byte as an unsigned 8-bit integer.Result is of type
shortbecause Java doesn't have unsigned types.- Returns:
- A value between 0 and 255, inclusive.
-
getUint16
public static int getUint16(byte[] bytes) Read 2 bytes as a big endian unsigned 16-bit integer.Result is of type
intbecause Java doesn't have unsigned types.- Returns:
- A value between 0 and 2^16- 1, inclusive.
-
getUint32
public static long getUint32(byte[] bytes) Read 4 bytes as a big endian unsigned 32-bit integer.Result is of type
longbecause Java doesn't have unsigned types.- Returns:
- A value between 0 and 2^32 - 1, inclusive.
-
encodeUint16
public static byte[] encodeUint16(int value) -
encodeUint32
public static byte[] encodeUint32(long value) -
readAll
- Throws:
IOException
-
encodeDerLength
public static byte[] encodeDerLength(int length) -
parseDerLength
public static BinaryUtil.ParseDerResult<Integer> parseDerLength(@NonNull @lombok.NonNull byte[] der, int offset) -
parseDerTaggedOrSkip
public static BinaryUtil.ParseDerResult<Optional<Integer>> parseDerTaggedOrSkip(@NonNull @lombok.NonNull byte[] der, int offset, byte expectTag, boolean constructed, BinaryUtil.DerTagClass expectTagClass) Parse a DER header with the given tag value, constructed bit and tag class, and return the start and end offsets of the value octets. If any of the three criteria do not match, return empty instead.- Parameters:
der- DER source to read from.offset- The offset inderfrom which to start reading.expectTag- The expected tag value, excluding the constructed bit and tag class. This is the 5 least significant bits of the tag octet.constructed- The expected "constructed" bit. This is bit 6 (the third-most significant bit) of the tag octet.expectTagClass- The expected tag class. This is the 2 most significant bits of the tag octet.- Returns:
- The start and end offsets of the value octets, if the parsed tag matches
expectTag,constructedandexpectTagClass, otherwise empty.BinaryUtil.ParseDerResult.nextOffsetis always returned.
-
parseDerSequenceContents
public static <T> BinaryUtil.ParseDerResult<List<T>> parseDerSequenceContents(@NonNull @lombok.NonNull byte[] der, int offset, int endOffset, @NonNull @NonNull BinaryUtil.ParseDerSequenceElementFunction<T> parseElement) Parse the elements of a SEQUENCE using the given element parsing function.- Parameters:
der- DER source array to read fromoffset- Offset from which to begin reading the first elementendOffset- Offset of the first octet past the end of the sequenceparseElement- Function to use to parse each element in the sequence.
-
parseDerSequence
public static <T> BinaryUtil.ParseDerResult<List<T>> parseDerSequence(@NonNull @lombok.NonNull byte[] der, int offset, @NonNull @NonNull BinaryUtil.ParseDerSequenceElementFunction<T> parseElement) Parse a SEQUENCE using the given element parsing function.- Parameters:
der- DER source array to read fromoffset- Offset from which to begin reading the SEQUENCEparseElement- Function to use to parse each element in the sequence.
-
parseDerOctetString
public static BinaryUtil.ParseDerResult<byte[]> parseDerOctetString(@NonNull @lombok.NonNull byte[] der, int offset) Parse an Octet String. -
encodeDerObjectId
public static byte[] encodeDerObjectId(@NonNull @lombok.NonNull byte[] oid) -
encodeDerBitStringWithZeroUnused
public static byte[] encodeDerBitStringWithZeroUnused(@NonNull @lombok.NonNull byte[] content) -
encodeDerSequence
public static byte[] encodeDerSequence(byte[]... items)
-