Class TOTPGenerator

java.lang.Object
org.cryptacular.generator.AbstractOTPGenerator
org.cryptacular.generator.TOTPGenerator

public class TOTPGenerator extends AbstractOTPGenerator
OTP generator component that implements the TOTP scheme described in RFC 6238.
  • Field Details

    • DEFAULT_START_TIME

      private static final Instant DEFAULT_START_TIME
      Default start time.
    • DEFAULT_TIME_STEP

      private static final Duration DEFAULT_TIME_STEP
      Default time step.
    • digestSpecification

      private final Spec<org.bouncycastle.crypto.Digest> digestSpecification
      Digest algorithm specification.
    • startTime

      private final Instant startTime
      Reference start time, T0. Default is 1970-01-01T00:00:00.
    • timeStep

      private final Duration timeStep
      Time step duration, X. Default is 30 seconds.
    • currentTime

      private Instant currentTime
      Current system time. This value is used if and only if it is a non-null value; otherwise the current system time is used.
  • Constructor Details

    • TOTPGenerator

      public TOTPGenerator()
      Creates a new TOTP generator that uses a SHA-1 digest.
    • TOTPGenerator

      public TOTPGenerator(int numberOfDigits)
      Creates a new TOTP generator that uses a SHA-1 digest.
      Parameters:
      numberOfDigits - Number of digits in generated OTP. MUST be in the range 6 - 9.
    • TOTPGenerator

      public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification)
      Creates a new TOTP generator.
      Parameters:
      specification - SHA-1, SHA-256, or SHA-512 digest specification.
    • TOTPGenerator

      public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits)
      Creates a new TOTP generator.
      Parameters:
      specification - SHA-1, SHA-256, or SHA-512 digest specification.
      numberOfDigits - Number of digits in generated OTP. MUST be in the range 6 - 9.
    • TOTPGenerator

      public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Instant startTime)
      Creates a new TOTP generator.
      Parameters:
      specification - SHA-1, SHA-256, or SHA-512 digest specification.
      numberOfDigits - Number of digits in generated OTP. MUST be in the range 6 - 9. Default is 6.
      startTime - Start time instant. Input is truncated to seconds.
    • TOTPGenerator

      public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Duration timeStep)
      Creates a new TOTP generator.
      Parameters:
      specification - SHA-1, SHA-256, or SHA-512 digest specification.
      numberOfDigits - Number of digits in generated OTP. MUST be in the range 6 - 9. Default is 6.
      timeStep - Time step duration. Default is 30s. This value determines the validity window of generated OTP values.
    • TOTPGenerator

      public TOTPGenerator(Spec<org.bouncycastle.crypto.Digest> specification, int numberOfDigits, Instant startTime, Duration timeStep)
      Creates a new TOTP generator.
      Parameters:
      specification - SHA-1, SHA-256, or SHA-512 digest specification.
      numberOfDigits - Number of digits in generated OTP. MUST be in the range 6 - 9. Default is 6.
      startTime - Start time instant. Input is truncated to seconds.
      timeStep - Time step duration. Default is 30s. This value determines the validity window of generated OTP values.
  • Method Details

    • getDigestSpecification

      public Spec<org.bouncycastle.crypto.Digest> getDigestSpecification()
      Returns:
      Digest algorithm used with the HMAC function.
    • getStartTime

      public Instant getStartTime()
      Returns:
      Reference start time.
    • getTimeStep

      public Duration getTimeStep()
      Returns:
      Time step in seconds.
    • generate

      public int generate(byte[] key)
      Generates the OTP given a per-user key.
      Parameters:
      key - Per-user key.
      Returns:
      Integer OTP.
    • getDigest

      protected org.bouncycastle.crypto.Digest getDigest()
      Specified by:
      getDigest in class AbstractOTPGenerator
      Returns:
      Digest algorithm used for HMAC operation.
    • setCurrentTime

      protected void setCurrentTime(Instant time)
      Sets the current time (supports testing). This value is used if and only if it is a non-negative value; otherwise the current system time is used.
      Parameters:
      time - to override the current time with
    • currentTime

      protected Instant currentTime()
      Returns:
      Current system time in seconds since the start of epoch, 1970-01-01T00:00:00.