Class TotpAlgorithm
java.lang.Object
com.irurueta.authserver.authorization.domain.totp.TotpAlgorithm
The time-based one-time password algorithm of RFC 6238 over HMAC-SHA1, with the parameters every authenticator
application supports: 30 second steps and 6 digits. It is pure and holds no state.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName of the algorithm as written in anotpauthURI.static final intNumber of digits of a code.private static final StringName of the JCA MAC algorithm.private static final intModulus that keeps the lastDIGITSdecimal digits.private static final intMask of the low nibble of the last hash byte, which gives the truncation offset.static final longLength of a time step, in seconds.private static final intMask that clears the sign bit of the truncated word.private static final intNumber of bytes of the truncated word.static final intNumber of steps accepted on each side of the current one, to tolerate clock drift. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerate(byte[] secret, long step) Computes the code of a time step.booleanTells whether a code is valid, with the window and replay rules ofmatchingStep(byte[], java.lang.String, java.time.Instant, long).matchingStep(byte[] secret, String code, Instant now, long lastUsedStep) Finds the time step a presented code corresponds to, acceptingWINDOWsteps on each side of the current one and rejecting any step that is not newer than the last one used, so that a code cannot be replayed.longComputes the time step that contains an instant.
-
Field Details
-
PERIOD_SECONDS
public static final long PERIOD_SECONDSLength of a time step, in seconds.- See Also:
-
DIGITS
public static final int DIGITSNumber of digits of a code.- See Also:
-
WINDOW
public static final int WINDOWNumber of steps accepted on each side of the current one, to tolerate clock drift.- See Also:
-
ALGORITHM_NAME
Name of the algorithm as written in anotpauthURI.- See Also:
-
MAC_ALGORITHM
Name of the JCA MAC algorithm.- See Also:
-
MODULUS
private static final int MODULUSModulus that keeps the lastDIGITSdecimal digits.- See Also:
-
OFFSET_MASK
private static final int OFFSET_MASKMask of the low nibble of the last hash byte, which gives the truncation offset.- See Also:
-
SIGN_MASK
private static final int SIGN_MASKMask that clears the sign bit of the truncated word.- See Also:
-
TRUNCATED_BYTES
private static final int TRUNCATED_BYTESNumber of bytes of the truncated word.- See Also:
-
-
Constructor Details
-
TotpAlgorithm
public TotpAlgorithm()
-
-
Method Details
-
generate
Computes the code of a time step.- Parameters:
secret- the shared secret.step- the time step, that is the Unix time divided byPERIOD_SECONDS.- Returns:
- the code, always
DIGITSdigits long, left padded with zeros. - Throws:
IllegalArgumentException- if the secret is empty.IllegalStateException- if the platform has no HMAC-SHA1.
-
stepAt
Computes the time step that contains an instant.- Parameters:
instant- the instant.- Returns:
- the step.
-
matchingStep
Finds the time step a presented code corresponds to, acceptingWINDOWsteps on each side of the current one and rejecting any step that is not newer than the last one used, so that a code cannot be replayed.- Parameters:
secret- the shared secret.code- the code presented by the user.now- current instant.lastUsedStep- the step of the last accepted code, orTotpCredential.NEVER_USED.- Returns:
- the matching step, or an empty value when the code is wrong, outside the window or already used.
-
matches
Tells whether a code is valid, with the window and replay rules ofmatchingStep(byte[], java.lang.String, java.time.Instant, long).- Parameters:
secret- the shared secret.code- the code presented by the user.now- current instant.lastUsedStep- the step of the last accepted code, orTotpCredential.NEVER_USED.- Returns:
truewhen the code is accepted.
-