public final class GoogleAuthenticator extends Object implements IGoogleAuthenticator
This class lets users create a new 16-bit base32-encoded secret key with
the validation code calculated at time = 0 (the UNIX epoch) and the
URL of a Google-provided QR barcode to let an user load the generated
information into Google Authenticator.
The random number generator used by this class uses the default algorithm and provider. Users can override them by setting the following system properties to the algorithm and provider name of their choice:
This class does not store in any way either the generated keys nor the keys passed during the authorization process.
Java Server side class for Google Authenticator's TOTP generator was inspired by an author's blog post.
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
RNG_ALGORITHM_KEY |
static String |
RNG_ALGORITHM_PROVIDER_KEY |
static int |
SCRATCH_CODE_MODULUS
Modulus used to truncate the scratch code.
|
| 构造器和说明 |
|---|
GoogleAuthenticator() |
GoogleAuthenticator(GoogleAuthenticatorConfig config) |
GoogleAuthenticator(GoogleAuthenticatorConfig config,
String randomNumberAlgorithm,
String randomNumberAlgorithmProvider) |
GoogleAuthenticator(String randomNumberAlgorithm,
String randomNumberAlgorithmProvider) |
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
authorize(String secret,
int verificationCode)
Checks a verification code against a secret key using the current time.
|
boolean |
authorize(String secret,
int verificationCode,
long time)
Checks a verification code against a secret key using the specified time.
|
GoogleAuthenticatorKey |
createCredentials()
This method generates a new set of credentials including:
Secret key.
|
GoogleAuthenticatorKey |
createCredentials(String userName)
This method generates a new set of credentials invoking the
#createCredentials method with no arguments. |
int |
getTotpPassword(String secret)
This method generates the current TOTP password.
|
int |
getTotpPassword(String secret,
long time)
This method generates the TOTP password at the specified time.
|
public static final int SCRATCH_CODE_MODULUS
public GoogleAuthenticator()
public GoogleAuthenticator(GoogleAuthenticatorConfig config)
public GoogleAuthenticator(String randomNumberAlgorithm, String randomNumberAlgorithmProvider)
public GoogleAuthenticator(GoogleAuthenticatorConfig config, String randomNumberAlgorithm, String randomNumberAlgorithmProvider)
public GoogleAuthenticatorKey createCredentials()
IGoogleAuthenticatorThe user must register this secret on their device.
createCredentials 在接口中 IGoogleAuthenticatorpublic GoogleAuthenticatorKey createCredentials(String userName)
IGoogleAuthenticator#createCredentials method with no arguments. The generated
credentials are then saved using the configured
#ICredentialRepository service.
The user must register this secret on their device.
createCredentials 在接口中 IGoogleAuthenticatoruserName - the user name.public int getTotpPassword(String secret)
IGoogleAuthenticatorgetTotpPassword 在接口中 IGoogleAuthenticatorsecret - the encoded secret key.public int getTotpPassword(String secret, long time)
IGoogleAuthenticatorgetTotpPassword 在接口中 IGoogleAuthenticatorsecret - The encoded secret key.time - The time to use to calculate the password.public boolean authorize(String secret, int verificationCode)
IGoogleAuthenticatorauthorize 在接口中 IGoogleAuthenticatorsecret - the encoded secret key.verificationCode - the verification code.true if the validation code is valid,
false otherwise.IGoogleAuthenticator.authorize(String, int, long)public boolean authorize(String secret, int verificationCode, long time)
IGoogleAuthenticatorwindowSize property of this class.
The default value of 30 seconds recommended by RFC 6238 is used for the interval size.
authorize 在接口中 IGoogleAuthenticatorsecret - The encoded secret key.verificationCode - The verification code.time - The time to use to calculate the TOTP password..true if the validation code is valid, false
otherwise.Copyright © 2025 fossc. All rights reserved.