Class SecretKeyGenerator

java.lang.Object
dev.vink.jtotp.SecretKeyGenerator

public final class SecretKeyGenerator extends Object
Utility class for generating secret keys for OTP (One-Time Password) authentication.

This class provides a method to generate a random secret key encoded in Base32 format.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Number of bits for generate function for SHA1 algorithm
    static final int
    Number of bits for generate function for SHA256 algorithm
    static final int
    Number of bits for generate function for SHA512 algorithm
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Generates a random secret key with the default bit length (160 bits for HmacSHA1).
    static String
    generate(int bits)
    Generates a random secret key encoded in Base32 format.

    Methods inherited from class java.lang.Object

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

    • SHA1_BITS

      public static final int SHA1_BITS
      Number of bits for generate function for SHA1 algorithm
      See Also:
    • SHA256_BITS

      public static final int SHA256_BITS
      Number of bits for generate function for SHA256 algorithm
      See Also:
    • SHA512_BITS

      public static final int SHA512_BITS
      Number of bits for generate function for SHA512 algorithm
      See Also:
  • Method Details

    • generate

      public static String generate(int bits)
      Generates a random secret key encoded in Base32 format.

      The generated key is suitable for use in OTP authentication systems.

      Parameters:
      bits - The bit length of the secret key (e.g., 160, 256, 512).
      Returns:
      A Base32-encoded secret key string with padding removed.
      Throws:
      IllegalArgumentException - If the bit length is invalid.
    • generate

      public static String generate()
      Generates a random secret key with the default bit length (160 bits for HmacSHA1).
      Returns:
      A Base32-encoded string representing the secret key.