Class Base32Codec

java.lang.Object
io.github.pratiyush.totp.internal.Base32Codec

public final class Base32Codec extends Object
RFC 4648 compliant Base32 encoder/decoder with zero external dependencies.

This implementation handles the standard Base32 alphabet (A-Z, 2-7) and supports both padded and unpadded input/output.

Features

  • RFC 4648 compliant encoding/decoding
  • Case-insensitive decoding
  • Optional padding handling
  • Strict validation mode
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    decode(String encoded)
    Decodes a Base32 string to binary data.
    static int
    decodedLength(int encodedLength)
    Returns the expected decoded size for a Base32 string length.
    static String
    encode(byte[] data)
    Encodes binary data to Base32 string without padding.
    static String
    encode(byte[] data, boolean padding)
    Encodes binary data to Base32 string.
    static int
    encodedLength(int inputBytes)
    Returns the expected output size for a given input size.
    static boolean
    isValid(String encoded)
    Validates a Base32 string without decoding.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • encode

      public static String encode(byte[] data)
      Encodes binary data to Base32 string without padding.
      Parameters:
      data - the data to encode
      Returns:
      Base32 encoded string (uppercase, no padding)
      Throws:
      NullPointerException - if data is null
    • encode

      public static String encode(byte[] data, boolean padding)
      Encodes binary data to Base32 string.
      Parameters:
      data - the data to encode
      padding - whether to include padding characters
      Returns:
      Base32 encoded string (uppercase)
      Throws:
      NullPointerException - if data is null
    • decode

      public static byte[] decode(String encoded) throws TOTPException
      Decodes a Base32 string to binary data.

      This method is lenient: it ignores whitespace, handles both padded and unpadded input, and is case-insensitive.

      Parameters:
      encoded - the Base32 string to decode
      Returns:
      decoded binary data
      Throws:
      TOTPException - if the string contains invalid characters
    • isValid

      public static boolean isValid(String encoded)
      Validates a Base32 string without decoding.
      Parameters:
      encoded - the string to validate
      Returns:
      true if the string is valid Base32
    • encodedLength

      public static int encodedLength(int inputBytes)
      Returns the expected output size for a given input size.
      Parameters:
      inputBytes - number of input bytes
      Returns:
      number of Base32 characters (without padding)
    • decodedLength

      public static int decodedLength(int encodedLength)
      Returns the expected decoded size for a Base32 string length.
      Parameters:
      encodedLength - number of Base32 characters (without padding)
      Returns:
      number of decoded bytes