Fixes #2079: Update "From Base32" operation to support strict mode and custom padding
Description
This implements #2079
The From Base64 operation allows for a strict mode, and also allows removing the padding character from the alphabet. The From Base32 operation, on the other hand, is not aligned with this design and produces an "Invalid byte array".
This change updates the Base32 operation to include a strict mode, which enforces the same rules as the Base64 tool (adhering to Base32 expectations instead of Base64, such as a maximum of 6 padding characters). In non-strict mode, this allows the user to enter inputs that are not divisible by 8 while still producing a valid output, even without a padding character.
This change also enables users to provide custom padding characters instead of forcing =.
Example:
- Input: "GE"
- Alphabet: "A-Z2-7"
Existing result:
Result after this change:
How it works
- Updates the default encoded character to be
nullinstead of=to allow for the absence of a padding character - Updates the Base32 logic to allow for a custom padding character instead of hard-coding
= - Only pushes a character to the byte array if it is between 0 and 256 to avoid pushing
-1and causing an invalid byte array - Strict mode forces the text to not be
5n + 1in length as it is an invalid length for a Base32 string.
Testing
All unit tests for Base32 continue to pass after this change. I also tested the following inputs and got valid results:
- "GE" with alphabet "A-Z2-7" produces output "1"
- "JBSWY3DP" with alphabet "A-Z2-7" produces "Hello"
- "O4" with alphabet "A-Z2-7" produces "04"
- "04=======" produces error "Error: Invalid Base32 input length (9). Cannot be 8n+1, even without padding chars."
- "..." produces error "Error: Base32 input contains non-alphabet char(s)"
References
See the Base64 implementation.
Checking in on this since it has been some time. How frequently does this repository have PRs merged?