Skip to content

Commit

Permalink
Comment padding
Browse files Browse the repository at this point in the history
  • Loading branch information
banterCZ committed Dec 20, 2023
1 parent 5a14440 commit 0a23550
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class IdentifierGenerator {
*/
private static final int ACTIVATION_CODE_BYTES_LENGTH = 12;

/**
* When {@code ACTIVATION_CODE_BYTES_LENGTH = 12}, the Base32 padding is always {@code ====}.
*/
private static final String ACTIVATION_CODE_PADDING = "====";

/**
* Default length of random bytes used for Activation Code.
* See {@link #generateActivationCode()} method for details.
Expand All @@ -67,8 +72,6 @@ public class IdentifierGenerator {
*/
private static final int PUK_DERIVATION_MAX_ATTEMPTS = 20;

private static final String PADDING = "====";

private final KeyGenerator keyGenerator = new KeyGenerator();
private final KeyConvertor keyConvertor = new KeyConvertor();

Expand Down Expand Up @@ -161,8 +164,8 @@ public boolean validateActivationCode(String activationCode) {
return false;
}

// Decode the Base32 value
byte[] activationCodeBytes = Base32.decode(activationCode.replace("-", "") + PADDING);
// The activation code does not contain the padding, but it must be present in the Base32 value to be valid.
byte[] activationCodeBytes = Base32.decode(activationCode.replace("-", "") + ACTIVATION_CODE_PADDING);

// Verify byte array length
if (activationCodeBytes.length != ACTIVATION_CODE_BYTES_LENGTH) {
Expand Down Expand Up @@ -359,7 +362,7 @@ private String generatePuk(SecretKey recoveryPukBaseKey, byte[] indexBytes) thro
* @return Base32 String representation of activation code.
*/
private String encodeActivationCode(final byte[] activationCodeBytes) {
// Generate Base32 representation from 12 activation code bytes
// Padding may be ignored; ACTIVATION_CODE_BYTES_LENGTH is set to 12 and the following substring takes only the first 20 characters.
final String base32Encoded = Base32.toBase32String(activationCodeBytes);

// Split Base32 string into 4 groups, each one contains 5 characters. Use "-" as separator.
Expand Down

0 comments on commit 0a23550

Please sign in to comment.