public final class GoogleAuthenticator extends Object implements Authenticator
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(AuthenticatorConfig config) |
GoogleAuthenticator(AuthenticatorConfig 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.
|
AuthenticatorKey |
createCredentials()
This method generates a new set of credentials including:
Secret key.
|
AuthenticatorKey |
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(AuthenticatorConfig config)
public GoogleAuthenticator(String randomNumberAlgorithm, String randomNumberAlgorithmProvider)
public GoogleAuthenticator(AuthenticatorConfig config, String randomNumberAlgorithm, String randomNumberAlgorithmProvider)
public AuthenticatorKey createCredentials()
AuthenticatorThe user must register this secret on their device.
createCredentials 在接口中 Authenticatorpublic AuthenticatorKey createCredentials(String userName)
Authenticator#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 在接口中 AuthenticatoruserName - the user name.public int getTotpPassword(String secret)
AuthenticatorgetTotpPassword 在接口中 Authenticatorsecret - the encoded secret key.public int getTotpPassword(String secret, long time)
AuthenticatorgetTotpPassword 在接口中 Authenticatorsecret - The encoded secret key.time - The time to use to calculate the password.public boolean authorize(String secret, int verificationCode)
Authenticatorauthorize 在接口中 Authenticatorsecret - the encoded secret key.verificationCode - the verification code.true if the validation code is valid,
false otherwise.Authenticator.authorize(String, int, long)public boolean authorize(String secret, int verificationCode, long time)
AuthenticatorwindowSize property of this class.
The default value of 30 seconds recommended by RFC 6238 is used for the interval size.
authorize 在接口中 Authenticatorsecret - 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.