Class SimpleTotpGenerator
java.lang.Object
es.kukenan.smartfi.common.utils.otp.SimpleTotpGenerator
Class to generate a Time-based One-time Password and PUC for bypassing the One-time Password for tests and PUC respectively.
The reference implementation for RFC-6238 is not working correctly with Google Authenticator. Google Authenticator is using an Base32 encoded String as secret key, while the reference implementation accepts a Hex encoded secret key. To be compatible with Google Authenticator the reference implementation is no longer used in this class. For your information: The reference implementation: https://tools.ietf.org/html/rfc6238#appendix-A
To support Base32 encoding, the algorithm from this site is used and is working with Google Authenticator: https://weblogs.java.net/blog/evanx/archive/2012/11/07/google-authenticator-thus-enabled
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intLowercase letter 'a'.private final Stringthe secret key.static final intLength.private final intKey validation time in seconds. -
Constructor Summary
ConstructorsConstructorDescriptionSimpleTotpGenerator(String secretKey, int validTimeInSeconds) Full state constructor. -
Method Summary
Modifier and TypeMethodDescriptionGenerates a TOTP based on a secret Key.private StringgenerateTotp(byte[] secret, long timeIndex) Generates a TPUC based on a secret key by converting a TOTP (only digits) into a PUC (first 2 and last 2 are letters).private intgetDigitValue(String pucInDigits, int characterIndex) private StringgetStringValue(int firstDigit) static voidEntrypoint for command line OTP generation.private String
-
Field Details
-
TOTP_TPUC_LENGTH
public static final int TOTP_TPUC_LENGTHLength.- See Also:
-
LOWERCASE_LETTER_A
private static final int LOWERCASE_LETTER_ALowercase letter 'a'.- See Also:
-
secretKey
the secret key. This should be a String containing the key and should NOT be Base32 encoded. However: the key added to Google Authenticator SHOULD be Base32 encoded. So plaintext key in this method. Same key encoded with Base32 in Google Authenticator. -
validTimeInSeconds
private final int validTimeInSecondsKey validation time in seconds. When unsure: use 30 (default for Google Authenticator).
-
-
Constructor Details
-
SimpleTotpGenerator
Full state constructor.- Parameters:
secretKey- the secret key. This should be a String containing the key and should NOT be Base32 encoded. However: the key added to Google Authenticator SHOULD be Base32 encoded. So plaintext key in this method. Same key encoded with Base32 in Google Authenticator.validTimeInSeconds- Key validation time in seconds. When unsure: use 30 (default for Google Authenticator).
-
-
Method Details
-
generateTotp
Generates a TOTP based on a secret Key.- Returns:
- the TOTP of length 6
-
generateTotp
private String generateTotp(byte[] secret, long timeIndex) throws NoSuchAlgorithmException, InvalidKeyException -
prefixWithZeroesWhenNeeded
-
generateTPuc
Generates a TPUC based on a secret key by converting a TOTP (only digits) into a PUC (first 2 and last 2 are letters). All lowercase.- Returns:
- the Time-based PUC of length 6. Format: LetterLetterDigitDigitLetterLetter. E.g: aa11bb
-
getDigitValue
-
getStringValue
-
main
Entrypoint for command line OTP generation.- Parameters:
args- arguments.
-