Class TOTPValidator
- java.lang.Object
-
- io.mangoo.crypto.totp.TOTPValidator
-
public final class TOTPValidator extends Object
A Time-based One-time Password (TOTP) validator.As per RFC 6238 (section 5.2):
"An OTP generated within the same time step will be the same. When an OTP is received at a validation system, it doesn't know a client's exact timestamp when an OTP was generated. The validation system may typically use the timestamp when an OTP is received for OTP comparison. Due to network latency, the gap (as measured by T, that is, the number of time steps since T0) between the time that the OTP was generated and the time that the OTP arrives at the receiving system may be large. The receiving time at the validation system and the actual OTP generation may not fall within the same time-step window that produced the same OTP. When an OTP is generated at the end of a time-step window, the receiving time most likely falls into the next time-step window. A validation system SHOULD typically set a policy for an acceptable OTP transmission delay window for validation. The validation system should compare OTPs not only with the receiving timestamp but also the past timestamps that are within the transmission delay. A larger acceptable delay window would expose a larger window for attacks. We RECOMMEND that at most one time step is allowed as the network delay."
Example:
// We will let the TOTP generation time == TOTP validation time so validation will succeed. final long time = System.currentTimeMillis(); byte[] key = "..."; TOTP totp = TOTP.key(key).build(time); boolean valid = TOTPValidator.window(0).isValid(key, totp.timeStep(), totp.digits(), totp.hmacShaAlgorithm(), totp.value(), time); // Should print "TOTP = ..., valid = true" System.out.printf("TOTP = %s, valid = %s%n", totp.value(), valid);- Author:
- Johnny Mongiat
- See Also:
- RFC 6238 (section 5.2)
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_WINDOWThe default window verification size.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TOTPValidatordefaultWindow()Returns a newTOTPValidatorinstance initialized with theDEFAULT_WINDOWverification size.booleanisValid(byte[] key, long timeStep, int digits, HmacShaAlgorithm hmacShaAlgorithm, String value)Returnstrueif the specified TOTPvaluematches the value of the TOTP generated at validation, otherwisefalse.booleanisValid(byte[] key, long timeStep, int digits, HmacShaAlgorithm hmacShaAlgorithm, String value, long validationTime)Returnstrueif the specified TOTPvaluematches the value of the TOTP generated at validation, otherwisefalse.static TOTPValidatorwindow(int window)Returns a newTOTPValidatorinstance initialized with the specifiedwindowverification size.
-
-
-
Field Detail
-
DEFAULT_WINDOW
public static final int DEFAULT_WINDOW
The default window verification size.- See Also:
- Constant Field Values
-
-
Method Detail
-
defaultWindow
public static TOTPValidator defaultWindow()
Returns a newTOTPValidatorinstance initialized with theDEFAULT_WINDOWverification size.- Returns:
- a new
TOTPValidatorinstance.
-
window
public static TOTPValidator window(int window)
Returns a newTOTPValidatorinstance initialized with the specifiedwindowverification size.- Parameters:
window- the window verification size- Returns:
- a new
TOTPValidatorinstance. - Throws:
IllegalArgumentException- ifwindowis < 0.
-
isValid
public boolean isValid(byte[] key, long timeStep, int digits, HmacShaAlgorithm hmacShaAlgorithm, String value)Returnstrueif the specified TOTPvaluematches the value of the TOTP generated at validation, otherwisefalse. The current system time (current time in milliseconds since the UNIX epoch) is used as the validation reference time.- Parameters:
key- the encoded shared secret keytimeStep- the time step size in millisecondsdigits- the number of digits a TOTP should containhmacShaAlgorithm-HmacShaAlgorithmvalue- the TOTP value to validate- Returns:
trueif the specified TOTPcodevalue matches the code value of the TOTP generated at validation, otherwisefalse.
-
isValid
public boolean isValid(byte[] key, long timeStep, int digits, HmacShaAlgorithm hmacShaAlgorithm, String value, long validationTime)Returnstrueif the specified TOTPvaluematches the value of the TOTP generated at validation, otherwisefalse.- Parameters:
key- the encoded shared secret keytimeStep- the time step size in millisecondsdigits- the number of digits a TOTP should containhmacShaAlgorithm-HmacShaAlgorithmvalue- the TOTP value to validatevalidationTime- the validation reference time in milliseconds- Returns:
trueif the specified TOTPcodevalue matches the code value of the TOTP generated at validation, otherwisefalse.
-
-