Class Base45
- All Implemented Interfaces:
BinaryDecoder, BinaryEncoder, Decoder, Encoder
Base45 is designed for efficient encoding of binary data in environments where a subset of ASCII characters is available, specifically 45 characters chosen from the QR code alphanumeric mode character set. Base45 is used in European Union Digital COVID Certificates (EUDCC) and similar applications.
The Base45 alphabet consists of 45 characters:
Value Encoding Value Encoding Value Encoding Value Encoding 0 0 12 C 24 O 36 Space 1 1 13 D 25 P 37 $ 2 2 14 E 26 Q 38 % 3 3 15 F 27 R 39 * 4 4 16 G 28 S 40 + 5 5 17 H 29 T 41 - 6 6 18 I 30 U 42 . 7 7 19 J 31 V 43 / 8 8 20 K 32 W 44 : 9 9 21 L 33 X 10 A 22 M 34 Y 11 B 23 N 35 Z
Encoding
Input bytes are grouped in pairs (2 bytes). Each pair is encoded as 3 Base45 characters. A single remaining byte is encoded as 2 Base45 characters. There is no padding.
- For each 2-byte pair
(b0, b1):n = b0 * 256 + b1; output 3 charactersalphabet[n % 45],alphabet[(n / 45) % 45],alphabet[n / 2025] - For a final single byte
b0:n = b0; output 2 charactersalphabet[n % 45],alphabet[n / 45]
Decoding
Input characters are grouped in triples (3 characters). Each triple decodes to 2 bytes. A pair of trailing characters decodes to 1 byte. An input whose length modulo 3 equals 1 is invalid.
This class is thread-safe.
To create an instance, use the default constructor or the builder:
Base45 codec = new Base45(); // Or, use the builder to customize the encode table: Base45 custom = Base45.builder().setEncodeTable(...).get();
- Since:
- 1.23.0
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class BaseNCodec
BaseNCodec.AbstractBuilder<T,B> -
Field Summary
Fields inherited from class BaseNCodec
DECODING_POLICY_DEFAULT, lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE -
Constructor Summary
ConstructorsConstructorDescriptionBase45()Constructs a Base45 codec using the default settings (strict decoding policy, the only supported policy). -
Method Summary
Modifier and TypeMethodDescriptionstatic Base45.Builderbuilder()Creates a newBase45.Builderfor configuring aBase45instance.longgetEncodedLength(byte[] array) Gets the number of Base45-encoded characters needed to encode the given byte array, as specified by RFC 9285.booleanisInAlphabet(byte value) Tests whether or not thevalueis a valid Base45 alphabet character.booleanisInAlphabet(byte[] arrayOctet, boolean allowWhitespacePad) Tests a given byte array to see if it contains only valid characters within the alphabet.Methods inherited from class BaseNCodec
containsAlphabetOrPad, decode, decode, decode, encode, encode, encode, encodeAsString, encodeToString, ensureBufferSize, getChunkSeparator, getCodecPolicy, getDefaultBufferSize, isInAlphabet, isStrictDecoding, isWhiteSpace
-
Constructor Details
-
Base45
public Base45()Constructs a Base45 codec using the default settings (strict decoding policy, the only supported policy).
-
-
Method Details
-
builder
Creates a newBase45.Builderfor configuring aBase45instance.- Returns:
- A new
Base45.Builder.
-
getEncodedLength
Gets the number of Base45-encoded characters needed to encode the given byte array, as specified by RFC 9285.The formula is:
(n / 2) * 3 + (n % 2 != 0 ? 2 : 0), wherenis the number of unencoded bytes.- Overrides:
getEncodedLengthin classBaseNCodec- Parameters:
array- The byte array to encode (used only for its length).- Returns:
- The number of Base45 characters that would be produced by encoding
array.
-
isInAlphabet
Tests whether or not thevalueis a valid Base45 alphabet character.- Specified by:
isInAlphabetin classBaseNCodec- Parameters:
value- The byte value to test.- Returns:
trueif the byte corresponds to a character in the Base45 alphabet (RFC 9285);falseotherwise.
-
isInAlphabet
Tests a given byte array to see if it contains only valid characters within the alphabet. The method optionally treats whitespace as valid.Unlike the
BaseNCodecimplementation, the pad character is not considered valid, because Base45 (RFC 9285) has no padding.- Overrides:
isInAlphabetin classBaseNCodec- Parameters:
arrayOctet- byte array to test.allowWhitespacePad- iftrue, then whitespace is also allowed.- Returns:
trueif all bytes are valid characters in the alphabet or if the byte array is empty;false, otherwise.
-