Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #554: Remove Guava dependency #569

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ 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 Down Expand Up @@ -164,8 +159,8 @@ public boolean validateActivationCode(String activationCode) {
return false;
}

// 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);
final String activationCodeBase32 = fetchActivationCodeBase32(activationCode);
final byte[] activationCodeBytes = Base32.decode(activationCodeBase32);

// Verify byte array length
if (activationCodeBytes.length != ACTIVATION_CODE_BYTES_LENGTH) {
Expand All @@ -184,6 +179,32 @@ public boolean validateActivationCode(String activationCode) {
return expectedChecksum == actualChecksum;
}

/**
* Remove hyphens and calculate padding.
* <p>
* When {@code ACTIVATION_CODE_BYTES_LENGTH = 12}, the Base32 padding is always {@code ====}, but this method is safe to change the length in the future.
*
* @param activationCode activation code with hyphens
* @return base32 with padding
*/
private static String fetchActivationCodeBase32(final String activationCode) {
final String activationCodeWithoutHyphens = activationCode.replace("-", "");
// The activation code does not contain the padding, but it must be present in the Base32 value to be valid.
final String activationCodePadding = switch (activationCodeWithoutHyphens.length() % 8) {
case 2:
yield "======";
case 4:
yield "====";
banterCZ marked this conversation as resolved.
Show resolved Hide resolved
case 5:
yield "===";
case 7:
yield "=";
default:
yield "";
};
return activationCodeWithoutHyphens + activationCodePadding;
}

/**
* Generate recovery code and PUK.
* @return Recovery code and PUK.
Expand Down
Loading