Class TotpUtils


  • public class TotpUtils
    extends Object
    Author:
    svenkubiak
    • Method Detail

      • createSecret

        public static String createSecret()
        Generates a 64 byte (512 bit) secret
        Returns:
        A 64 characters random string based on SecureRandom
      • getTotp

        public static String getTotp​(String secret)
        Creates the current TOTP based on the following default values: SHA512 algorithm, 6 digits, 30 seconds time period
        Parameters:
        secret - The secret to use
        Returns:
        The totp value or null if generation failed
      • getTotp

        public static String getTotp​(String secret,
                                     HmacShaAlgorithm algorithm,
                                     int digits,
                                     int period)
        Creates the current TOTP based on the given parameters
        Parameters:
        secret - The secret to use
        algorithm - The algorithm to use
        digits - The digits to use (6 or 8)
        period - The time period in seconds
        Returns:
        The totp value or null if generation failed
      • verifiedTotp

        public static boolean verifiedTotp​(String secret,
                                           String totp)
        Verifies a given TOTP based on the following default values: SHA512 algorithm, 6 digits, 30 seconds time period
        Parameters:
        secret - The secret to use
        totp - The TOTP to verify
        Returns:
        True if the TOTP is valid, false otherwise
      • getQRCode

        public static String getQRCode​(String name,
                                       String issuer,
                                       String secret,
                                       HmacShaAlgorithm algorithm,
                                       String digits,
                                       String period)
        Generates a QR code link from google charts API to share a secret with a user
        Parameters:
        name - The name of the account
        issuer - The name of the issuer
        secret - The secret to use
        algorithm - The algorithm to use
        digits - The number of digits to use
        period - The period to use
        Returns:
        An URL to Google charts API with the QR code
      • getOtpauthURL

        public static String getOtpauthURL​(String name,
                                           String issuer,
                                           String secret,
                                           HmacShaAlgorithm algorithm,
                                           String digits,
                                           String period)
        Generates a otpauth code to share a secret with a user
        Parameters:
        name - The name of the account
        issuer - The name of the issuer
        secret - The secret to use
        algorithm - The algorithm to use
        digits - The number of digits to use
        period - The period to use
        Returns:
        An otpauth url