Class Base32Codec
java.lang.Object
io.github.pratiyush.totp.internal.Base32Codec
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 TypeMethodDescriptionstatic byte[]Decodes a Base32 string to binary data.static intdecodedLength(int encodedLength) Returns the expected decoded size for a Base32 string length.static Stringencode(byte[] data) Encodes binary data to Base32 string without padding.static Stringencode(byte[] data, boolean padding) Encodes binary data to Base32 string.static intencodedLength(int inputBytes) Returns the expected output size for a given input size.static booleanValidates a Base32 string without decoding.
-
Method Details
-
encode
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
Encodes binary data to Base32 string.- Parameters:
data- the data to encodepadding- whether to include padding characters- Returns:
- Base32 encoded string (uppercase)
- Throws:
NullPointerException- if data is null
-
decode
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
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
-