Class DefaultCodeGenerator

java.lang.Object
com.iizix.totp.DefaultCodeGenerator
All Implemented Interfaces:
CodeGenerator

public class DefaultCodeGenerator extends Object implements CodeGenerator
The RFC 4226 / RFC 6238 code generator: HMAC of the big-endian 8-byte counter with the Base32-decoded secret, dynamic truncation, modulo 10^digits, zero-padded.
  • Constructor Details

    • DefaultCodeGenerator

      public DefaultCodeGenerator()
      Constructs a generator with the defaults: SHA256 and 6 digits.
    • DefaultCodeGenerator

      public DefaultCodeGenerator(HashingAlgorithm algorithm)
      Constructs a 6 digit generator.
      Parameters:
      algorithm - The hashing algorithm.
      Throws:
      NullPointerException - If algorithm is null.
    • DefaultCodeGenerator

      public DefaultCodeGenerator(HashingAlgorithm algorithm, int digits)
      Constructs a generator.
      Parameters:
      algorithm - The hashing algorithm.
      digits - The number of digits, at least 1.
      Throws:
      NullPointerException - If algorithm is null.
      IllegalArgumentException - If digits is less than 1.
  • Method Details

    • generate

      public String generate(String key, long counter) throws CodeGenerationException
      Specified by:
      generate in interface CodeGenerator
      Parameters:
      key - The shared secret/key to generate the code with.
      counter - The current time bucket number. Number of seconds since epoch / bucket period.
      Returns:
      The n-digit code for the secret/counter.
      Throws:
      CodeGenerationException - Thrown if the code generation fails for any reason.