Class TOTPGenerator


public class TOTPGenerator
extends OneTimePasswordGenerator
Generates time-based one-time passwords
Author:
Bastiaan Jansen
See Also:
OneTimePasswordGenerator
  • Field Details

    • DEFAULT_PERIOD

      public static final java.time.Duration DEFAULT_PERIOD
      Default time interval for a time-based one-time password
  • Constructor Details

    • TOTPGenerator

      public TOTPGenerator​(String secret)
      Constructs generator
      Parameters:
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(int passwordLength, String secret)
      Constructs generator with custom password length
      Parameters:
      passwordLength - number of digits for generated code in range 6...8
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(java.time.Duration period, String secret)
      Constructs generator with custom time interval
      Parameters:
      period - time interval between new codes
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(java.time.Duration period, HMACAlgorithm algorithm, String secret)
      Constructs generator with custom time interval and hashing algorithm
      Parameters:
      period - time interval between new codes
      algorithm - HMAC hash algorithm used to hash data
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(HMACAlgorithm algorithm, String secret)
      Constructs generator with custom hashing algorithm and default time interval
      Parameters:
      algorithm - HMAC hash algorithm used to hash data
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(int passwordLength, java.time.Duration period, String secret)
      Constructs generator with custom password length and period
      Parameters:
      passwordLength - number of digits for generated code in range 6...8
      period - time interval between new codes
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(int passwordLength, HMACAlgorithm algorithm, String secret)
      Constructs generator with custom password length and hashing algorithm
      Parameters:
      passwordLength - number of digits for generated code in range 6...8
      algorithm - HMAC hash algorithm used to hash data
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(int passwordLength, java.time.Duration period, HMACAlgorithm algorithm, String secret)
      Constructs generator with custom password length, time interval and hashing algorithm
      Parameters:
      passwordLength - number of digits for generated code in range 6...8
      period - time interval between new codes
      algorithm - HMAC hash algorithm used to hash data
      secret - used to generate hash
    • TOTPGenerator

      public TOTPGenerator​(URI uri) throws UnsupportedEncodingException
      Constructs generator from a OTPAuth URI
      Parameters:
      uri - OTPAuth URI
      Throws:
      UnsupportedEncodingException - when URI query can't be encoded
  • Method Details

    • generate

      public String generate() throws IllegalStateException
      Generate a time-based one-time password for current time interval instant
      Returns:
      generated TOTP code
      Throws:
      IllegalStateException - when code could not be generated
    • generate

      public String generate​(java.time.Instant instant) throws IllegalStateException
      Generate a time-based one-time password for a Java instant
      Parameters:
      instant - an instant
      Returns:
      generated TOTP code
      Throws:
      IllegalStateException - when code could not be generated
    • generate

      public String generate​(Date date) throws IllegalStateException
      Generate a time-based one-time password for a specific date
      Parameters:
      date - specific date
      Returns:
      generated TOTP code
      Throws:
      IllegalStateException - when code could not be generated
    • generate

      public String generate​(long secondsPast1970) throws IllegalArgumentException
      Generate a time-based one-time password for a specific time based on seconds past 1970
      Parameters:
      secondsPast1970 - seconds past 1970
      Returns:
      generated TOTP code
      Throws:
      IllegalArgumentException - when code could not be generated
    • verify

      public boolean verify​(String code)
      Checks whether a code is valid for a specific counter
      Parameters:
      code - an OTP code
      Returns:
      a boolean, true if code is valid, otherwise false
    • verify

      public boolean verify​(String code, int delayWindow)
      Checks whether a code is valid for a specific counter taking a delay window into account
      Parameters:
      code - an OTP code
      delayWindow - window in which a code can still be deemed valid
      Returns:
      a boolean, true if code is valid, otherwise false
    • getPeriod

      public java.time.Duration getPeriod()
    • getURI

      public URI getURI​(String issuer) throws URISyntaxException
      Create a OTPAuth URI for easy onboarding with only an issuer
      Parameters:
      issuer - name
      Returns:
      generated OTPAuth URI
      Throws:
      URISyntaxException - when URI cannot be created
    • getURI

      public URI getURI​(String issuer, String account) throws URISyntaxException
      Create a OTPAuth URI for easy user on-boarding with an issuer and account name
      Parameters:
      issuer - name
      account - name
      Returns:
      generated OTPAuth URI
      Throws:
      URISyntaxException - when URI cannot be created