Class OTPGenerator

java.lang.Object
com.bastiaanjansen.otp.OTPGenerator
Direct Known Subclasses:
HOTPGenerator, TOTPGenerator

public class OTPGenerator extends Object
Generates one-time passwords
Author:
Bastiaan Jansen
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    Abstract OTP builder
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final HMACAlgorithm
    Hashing algorithm used to generate code, defaults to SHA1
    protected final int
    Number of digits for generated code in range 6...8, defaults to 6
    protected final byte[]
    Secret key used to generate the code, this should be a base32 string
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    OTPGenerator(int passwordLength, HMACAlgorithm algorithm, byte[] secret)
    Constructs the generator with custom password length and hashing algorithm
  • Method Summary

    Modifier and Type
    Method
    Description
    protected String
    generateCode(long counter)
    Generate a code
     
    int
     
    byte[]
     
    protected URI
    getURI(String type, String path, Map<String,String> query)
    Generate an OTPAuth URI
    boolean
    verify(String code, long counter)
    Checks whether a code is valid for a specific counter with a delay window of 0
    boolean
    verify(String code, long counter, int delayWindow)
    Checks whether a code is valid for a specific counter taking a delay window into account

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • passwordLength

      protected final int passwordLength
      Number of digits for generated code in range 6...8, defaults to 6
    • algorithm

      protected final HMACAlgorithm algorithm
      Hashing algorithm used to generate code, defaults to SHA1
    • secret

      protected final byte[] secret
      Secret key used to generate the code, this should be a base32 string
  • Constructor Details

    • OTPGenerator

      protected OTPGenerator(int passwordLength, HMACAlgorithm algorithm, byte[] secret)
      Constructs the 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
  • Method Details

    • getPasswordLength

      public int getPasswordLength()
    • getAlgorithm

      public HMACAlgorithm getAlgorithm()
    • getSecret

      public byte[] getSecret()
    • verify

      public boolean verify(String code, long counter)
      Checks whether a code is valid for a specific counter with a delay window of 0
      Parameters:
      code - an OTP code
      counter - how many times time interval has passed since 1970
      Returns:
      a boolean, true if code is valid, otherwise false
    • verify

      public boolean verify(String code, long counter, int delayWindow)
      Checks whether a code is valid for a specific counter taking a delay window into account
      Parameters:
      code - an OTP codee
      counter - how many times time interval has passed since 1970
      delayWindow - window in which a code can still be deemed valid
      Returns:
      a boolean, true if code is valid, otherwise false
    • generateCode

      protected String generateCode(long counter) throws IllegalStateException
      Generate a code
      Parameters:
      counter - how many times time interval has passed since 1970
      Returns:
      generated OTP code
      Throws:
      IllegalStateException - when hashing algorithm throws an error
    • getURI

      protected URI getURI(String type, String path, Map<String,String> query) throws URISyntaxException
      Generate an OTPAuth URI
      Parameters:
      type - of OTPAuth URI: totp or hotp
      path - contains issuer and account name
      query - items of URI
      Returns:
      created OTPAuth URI
      Throws:
      URISyntaxException - when URI cannot be created