Class Base45

java.lang.Object
org.apache.commons.codec.binary.BaseNCodec
org.apache.commons.codec.binary.Base45
All Implemented Interfaces:
BinaryDecoder, BinaryEncoder, Decoder, Encoder

public class Base45 extends BaseNCodec
Provides Base45 encoding and decoding as defined by RFC 9285.

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 characters alphabet[n % 45], alphabet[(n / 45) % 45], alphabet[n / 2025]
  • For a final single byte b0: n = b0; output 2 characters alphabet[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:
  • Constructor Details

    • Base45

      public Base45()
      Constructs a Base45 codec using the default settings (strict decoding policy, the only supported policy).
  • Method Details

    • builder

      public static Base45.Builder builder()
      Creates a new Base45.Builder for configuring a Base45 instance.
      Returns:
      A new Base45.Builder.
    • getEncodedLength

      public long getEncodedLength(byte[] array)
      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), where n is the number of unencoded bytes.

      Overrides:
      getEncodedLength in class BaseNCodec
      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

      public boolean isInAlphabet(byte value)
      Tests whether or not the value is a valid Base45 alphabet character.
      Specified by:
      isInAlphabet in class BaseNCodec
      Parameters:
      value - The byte value to test.
      Returns:
      true if the byte corresponds to a character in the Base45 alphabet (RFC 9285); false otherwise.
    • isInAlphabet

      public boolean isInAlphabet(byte[] arrayOctet, boolean allowWhitespacePad)
      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 BaseNCodec implementation, the pad character is not considered valid, because Base45 (RFC 9285) has no padding.

      Overrides:
      isInAlphabet in class BaseNCodec
      Parameters:
      arrayOctet - byte array to test.
      allowWhitespacePad - if true, then whitespace is also allowed.
      Returns:
      true if all bytes are valid characters in the alphabet or if the byte array is empty; false, otherwise.