public class TimeBasedOneTimePasswordUtil extends Object
Whenever the user logs in:
See: https://github.com/j256/two-factor-auth
For more details about this magic algorithm, see: http://en.wikipedia.org/wiki/Time-based_One-time_Password_Algorithm
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_OTP_LENGTH
default number of digits in a OTP string
|
static int |
DEFAULT_QR_DIMENTION
default hight/width of QR image
|
static int |
DEFAULT_TIME_STEP_SECONDS
default time-step which is part of the spec, 30 seconds is default
|
| Constructor and Description |
|---|
TimeBasedOneTimePasswordUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
generateBase32Secret()
Generate and return a 16-character secret key in base32 format (A-Z2-7) using
SecureRandom. |
static String |
generateBase32Secret(int numDigits)
Similar to
generateBase32Secret() but specifies a character length. |
static int |
generateCurrentNumber(String base32Secret)
Similar to
generateCurrentNumberString(String) but this returns a int instead of a string. |
static int |
generateCurrentNumber(String base32Secret,
int numDigits)
Similar to
generateCurrentNumberString(String, int) but this returns a int instead of a string. |
static int |
generateCurrentNumberHex(String hexSecret)
Similar to
generateCurrentNumberStringHex(String) but this returns a int instead of a string. |
static int |
generateCurrentNumberHex(String hexSecret,
int numDigits)
Similar to
generateCurrentNumberStringHex(String, int) but this returns a int instead of a string. |
static String |
generateCurrentNumberString(String base32Secret)
Return the current number to be checked.
|
static String |
generateCurrentNumberString(String base32Secret,
int numDigits)
Similar to
generateCurrentNumberString(String, int) but you specify the number of digits. |
static String |
generateCurrentNumberStringHex(String hexSecret)
Similar to
generateCurrentNumberString(String) except this uses a hexadecimal secret. |
static String |
generateCurrentNumberStringHex(String hexSecret,
int numDigits)
Similar to
generateCurrentNumberString(String, int) but you specify the number of digits. |
static String |
generateHexSecret()
Generate and return a 32-character secret key in hexadecimal format (0-9A-F) using
SecureRandom. |
static String |
generateHexSecret(int numDigits)
Similar to
generateHexSecret() but specifies a character length. |
static int |
generateNumber(String base32Secret,
long timeMillis,
int timeStepSeconds)
Similar to
generateNumberString(String, long, int, int) but this returns a int instead of a string. |
static int |
generateNumber(String base32Secret,
long timeMillis,
int timeStepSeconds,
int numDigits)
Similar to
#generateNumberString(String, long, int) but this returns a int instead of a string. |
static int |
generateNumberHex(String hexSecret,
long timeMillis,
int timeStepSeconds)
Similar to
#generateNumberStringHex(String, long, int, int)) but this returns a int instead of a string. |
static int |
generateNumberHex(String hexSecret,
long timeMillis,
int timeStepSeconds,
int numDigits)
Similar to
generateNumber(String, long, int, int) but with a hexadecimal secret. |
static String |
generateNumberString(String base32Secret,
long timeMillis,
int timeStepSeconds,
int numDigits)
Similar to
generateCurrentNumberString(String) except exposes other parameters. |
static String |
generateNumberStringHex(String hexSecret,
long timeMillis,
int timeStepSeconds,
int numDigits)
Similar to
generateNumberStringHex(String, long, int, int) except it uses a hexadecimal secret. |
static String |
generateOtpAuthUrl(String keyId,
String secret)
Return the otp-auth part of the QR image which is suitable to be injected into other QR generators (e.g.
|
static String |
generateOtpAuthUrl(String keyId,
String secret,
int numDigits)
Return the otp-auth part of the QR image which is suitable to be injected into other QR generators (e.g.
|
static String |
qrImageUrl(String keyId,
String secret)
Return the QR image url thanks to Google.
|
static String |
qrImageUrl(String keyId,
String secret,
int numDigits)
Return the QR image url thanks to Google.
|
static String |
qrImageUrl(String keyId,
String secret,
int numDigits,
int imageDimension)
Return the QR image url thanks to Google.
|
static boolean |
validateCurrentNumber(String base32Secret,
int authNumber,
long windowMillis)
Validate a given secret-number using the secret base-32 string.
|
static boolean |
validateCurrentNumber(String base32Secret,
int authNumber,
long windowMillis,
long timeMillis,
int timeStepSeconds)
Similar to
#validateCurrentNumber(String, int, int) except exposes other parameters. |
static boolean |
validateCurrentNumber(String base32Secret,
int authNumber,
long windowMillis,
long timeMillis,
int timeStepSeconds,
int numDigits)
Similar to
#validateCurrentNumber(String, int, int) except exposes other parameters. |
static boolean |
validateCurrentNumberHex(String hexSecret,
int authNumber,
long windowMillis)
Similar to
validateCurrentNumber(String, int, long) except it uses a hexadecimal secret instead of
base-32. |
static boolean |
validateCurrentNumberHex(String hexSecret,
int authNumber,
long windowMillis,
long timeMillis,
int timeStepSeconds)
Similar to
#validateCurrentNumberHex(String, int, int) except exposes other parameters. |
static boolean |
validateCurrentNumberHex(String hexSecret,
int authNumber,
long windowMillis,
long timeMillis,
int timeStepSeconds,
int numDigits)
Similar to
validateCurrentNumber(String, int, long, long, int, int) except it uses hexadecimal secret
instead of base-32. |
public static final int DEFAULT_TIME_STEP_SECONDS
public static int DEFAULT_OTP_LENGTH
public static int DEFAULT_QR_DIMENTION
public static String generateBase32Secret()
SecureRandom. Could be used
to generate the QR image to be shared with the user. Other lengths should use generateBase32Secret(int).public static String generateBase32Secret(int numDigits)
generateBase32Secret() but specifies a character length.public static String generateHexSecret()
SecureRandom. Could be
used to generate the QR image to be shared with the user. Other lengths should use
generateHexSecret(int).public static String generateHexSecret(int numDigits)
generateHexSecret() but specifies a character length.public static boolean validateCurrentNumber(String base32Secret, int authNumber, long windowMillis) throws GeneralSecurityException
WARNING: This requires a system clock that is in sync with the world.
base32Secret - Secret string encoded using base-32 that was used to generate the QR code or shared with the user.authNumber - Time based number provided by the user from their authenticator application.windowMillis - Number of milliseconds that they are allowed to be off and still match. This checks before and after
the current time to account for clock variance. Set to 0 for no window.GeneralSecurityExceptionpublic static boolean validateCurrentNumberHex(String hexSecret, int authNumber, long windowMillis) throws GeneralSecurityException
validateCurrentNumber(String, int, long) except it uses a hexadecimal secret instead of
base-32.hexSecret - Secret string encoded in hexadecimal that was used to generate the QR code or shared with the user.authNumber - Time based number provided by the user from their authenticator application.windowMillis - Number of milliseconds that they are allowed to be off and still match. This checks before and after
the current time to account for clock variance. Set to 0 for no window.GeneralSecurityExceptionpublic static boolean validateCurrentNumber(String base32Secret, int authNumber, long windowMillis, long timeMillis, int timeStepSeconds) throws GeneralSecurityException
#validateCurrentNumber(String, int, int) except exposes other parameters. Mostly for testing.base32Secret - Secret string encoded using base-32 that was used to generate the QR code or shared with the user.authNumber - Time based number provided by the user from their authenticator application.windowMillis - Number of milliseconds that they are allowed to be off and still match. This checks before and after
the current time to account for clock variance. Set to 0 for no window.timeMillis - Time in milliseconds.timeStepSeconds - Time step in seconds. The default value is 30 seconds here. See DEFAULT_TIME_STEP_SECONDS.GeneralSecurityExceptionpublic static boolean validateCurrentNumberHex(String hexSecret, int authNumber, long windowMillis, long timeMillis, int timeStepSeconds) throws GeneralSecurityException
#validateCurrentNumberHex(String, int, int) except exposes other parameters. Mostly for
testing.hexSecret - Secret string encoded in hexadecimal that was used to generate the QR code or shared with the user.authNumber - Time based number provided by the user from their authenticator application.windowMillis - Number of milliseconds that they are allowed to be off and still match. This checks before and after
the current time to account for clock variance. Set to 0 for no window.timeMillis - Time in milliseconds.timeStepSeconds - Time step in seconds. The default value is 30 seconds here. See DEFAULT_TIME_STEP_SECONDS.GeneralSecurityExceptionpublic static boolean validateCurrentNumber(String base32Secret, int authNumber, long windowMillis, long timeMillis, int timeStepSeconds, int numDigits) throws GeneralSecurityException
#validateCurrentNumber(String, int, int) except exposes other parameters. Mostly for testing.base32Secret - Secret string encoded using base-32 that was used to generate the QR code or shared with the user.authNumber - Time based number provided by the user from their authenticator application.windowMillis - Number of milliseconds that they are allowed to be off and still match. This checks before and after
the current time to account for clock variance. Set to 0 for no window.timeMillis - Time in milliseconds.timeStepSeconds - Time step in seconds. The default value is 30 seconds here. See DEFAULT_TIME_STEP_SECONDS.numDigits - The number of digits of the OTP.GeneralSecurityExceptionpublic static boolean validateCurrentNumberHex(String hexSecret, int authNumber, long windowMillis, long timeMillis, int timeStepSeconds, int numDigits) throws GeneralSecurityException
validateCurrentNumber(String, int, long, long, int, int) except it uses hexadecimal secret
instead of base-32.hexSecret - Secret string encoded in hexadecimal that was used to generate the QR code or shared with the user.authNumber - Time based number provided by the user from their authenticator application.windowMillis - Number of milliseconds that they are allowed to be off and still match. This checks before and after
the current time to account for clock variance. Set to 0 for no window.timeMillis - Time in milliseconds.timeStepSeconds - Time step in seconds. The default value is 30 seconds here. See DEFAULT_TIME_STEP_SECONDS.numDigits - The number of digits of the OTP.GeneralSecurityExceptionpublic static String generateCurrentNumberString(String base32Secret) throws GeneralSecurityException
WARNING: This requires a system clock that is in sync with the world.
base32Secret - Secret string encoded using base-32 that was used to generate the QR code or shared with the user.GeneralSecurityExceptionpublic static String generateCurrentNumberStringHex(String hexSecret) throws GeneralSecurityException
generateCurrentNumberString(String) except this uses a hexadecimal secret.hexSecret - Secret string encoded in hexadecimal that was used to generate the QR code or shared with the user.GeneralSecurityExceptionpublic static String generateCurrentNumberString(String base32Secret, int numDigits) throws GeneralSecurityException
generateCurrentNumberString(String, int) but you specify the number of digits.base32Secret - Secret string encoded using base-32 that was used to generate the QR code or shared with the user.numDigits - The number of digits of the OTP.GeneralSecurityExceptionpublic static String generateCurrentNumberStringHex(String hexSecret, int numDigits) throws GeneralSecurityException
generateCurrentNumberString(String, int) but you specify the number of digits.hexSecret - Secret string encoded in hexadecimal that was used to generate the QR code or shared with the user.numDigits - The number of digits of the OTP.GeneralSecurityExceptionpublic static String generateNumberString(String base32Secret, long timeMillis, int timeStepSeconds, int numDigits) throws GeneralSecurityException
generateCurrentNumberString(String) except exposes other parameters. Mostly for testing.base32Secret - Secret string encoded using base-32 that was used to generate the QR code or shared with the user.timeMillis - Time in milliseconds.timeStepSeconds - Time step in seconds. The default value is 30 seconds here. See DEFAULT_TIME_STEP_SECONDS.numDigits - The number of digits of the OTP.GeneralSecurityExceptionpublic static String generateNumberStringHex(String hexSecret, long timeMillis, int timeStepSeconds, int numDigits) throws GeneralSecurityException
generateNumberStringHex(String, long, int, int) except it uses a hexadecimal secret.hexSecret - Secret string encoded in hexadecimal that was used to generate the QR code or shared with the user.timeMillis - Time in milliseconds.timeStepSeconds - Time step in seconds. The default value is 30 seconds here. See DEFAULT_TIME_STEP_SECONDS.numDigits - The number of digits of the OTP.GeneralSecurityExceptionpublic static int generateCurrentNumber(String base32Secret) throws GeneralSecurityException
generateCurrentNumberString(String) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateCurrentNumberHex(String hexSecret) throws GeneralSecurityException
generateCurrentNumberStringHex(String) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateCurrentNumber(String base32Secret, int numDigits) throws GeneralSecurityException
generateCurrentNumberString(String, int) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateCurrentNumberHex(String hexSecret, int numDigits) throws GeneralSecurityException
generateCurrentNumberStringHex(String, int) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateNumber(String base32Secret, long timeMillis, int timeStepSeconds) throws GeneralSecurityException
generateNumberString(String, long, int, int) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateNumberHex(String hexSecret, long timeMillis, int timeStepSeconds) throws GeneralSecurityException
#generateNumberStringHex(String, long, int, int)) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateNumber(String base32Secret, long timeMillis, int timeStepSeconds, int numDigits) throws GeneralSecurityException
#generateNumberString(String, long, int) but this returns a int instead of a string.GeneralSecurityExceptionpublic static int generateNumberHex(String hexSecret, long timeMillis, int timeStepSeconds, int numDigits) throws GeneralSecurityException
generateNumber(String, long, int, int) but with a hexadecimal secret.GeneralSecurityExceptionpublic static String qrImageUrl(String keyId, String secret)
keyId - Name of the key that you want to show up in the users authentication application. Should already be
URL encoded.secret - Secret string that will be used when generating the current number.public static String qrImageUrl(String keyId, String secret, int numDigits)
keyId - Name of the key that you want to show up in the users authentication application. Should already be
URL encoded.secret - Secret string that will be used when generating the current number.numDigits - The number of digits of the OTP.public static String qrImageUrl(String keyId, String secret, int numDigits, int imageDimension)
keyId - Name of the key that you want to show up in the users authentication application. Should already be
URL encoded.secret - Secret string that will be used when generating the current number.numDigits - The number of digits of the OTP. Can be set to DEFAULT_OTP_LENGTH.imageDimension - The dimension of the image, width and height. Can be set to DEFAULT_QR_DIMENTION.public static String generateOtpAuthUrl(String keyId, String secret)
keyId - Name of the key that you want to show up in the users authentication application. Should already be
URL encoded.secret - Secret string that will be used when generating the current number.public static String generateOtpAuthUrl(String keyId, String secret, int numDigits)
keyId - Name of the key that you want to show up in the users authentication application. Should already be
URL encoded.secret - Secret string that will be used when generating the current number.numDigits - The number of digits" of the OTP.This content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.