java.lang.Object
io.getlime.security.powerauth.crypto.lib.totp.Totp

public final class Totp extends Object
TOTP: Time-Based One-Time Password Algorithm according to RFC 6238.
Author:
Lubos Racansky, lubos.racansky@wultra.com
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    generateTotpSha256(byte[] key, Instant instant, int digitsNumber)
    Generates a TOTP value for the given set of parameters using HmacSHA256 algorithm.
    static byte[]
    generateTotpSha256(byte[] key, Instant instant, Duration stepLength, int digitsNumber)
    Generates a TOTP value for the given set of parameters using HmacSHA256 algorithm.
    static byte[]
    generateTotpSha512(byte[] key, Instant instant, int digitsNumber)
    Generates a TOTP value for the given set of parameters using HmacSHA512 algorithm.
    static byte[]
    generateTotpSha512(byte[] key, Instant instant, Duration stepLength, int digitsNumber)
    Generates a TOTP value for the given set of parameters using HmacSHA512 algorithm.
    static boolean
    validateTotpSha256(byte[] otp, byte[] key, Instant instant, int digitsNumber)
    Validate a TOTP value for the given set of parameters using HmacSHA256 algorithm.
    static boolean
    validateTotpSha256(byte[] otp, byte[] key, Instant instant, int digitsNumber, int steps, Duration stepLength)
    Validate a TOTP value for the given set of parameters using HmacSHA256 algorithm.
    static boolean
    validateTotpSha512(byte[] otp, byte[] key, Instant instant, int digitsNumber)
    Validate a TOTP value for the given set of parameters using HmacSHA512 algorithm.
    static boolean
    validateTotpSha512(byte[] otp, byte[] key, Instant instant, int digitsNumber, int steps, Duration stepLength)
    Validate a TOTP value for the given set of parameters using HmacSHA512 algorithm.

    Methods inherited from class java.lang.Object

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

    • generateTotpSha256

      public static byte[] generateTotpSha256(byte[] key, Instant instant, int digitsNumber) throws CryptoProviderException
      Generates a TOTP value for the given set of parameters using HmacSHA256 algorithm.
      Parameters:
      key - the shared secret
      instant - current Unix time
      digitsNumber - number of digits to return
      Returns:
      a numeric String in base 10 that includes truncation digits
      Throws:
      CryptoProviderException - in case of any crypto error
    • generateTotpSha256

      public static byte[] generateTotpSha256(byte[] key, Instant instant, Duration stepLength, int digitsNumber) throws CryptoProviderException
      Generates a TOTP value for the given set of parameters using HmacSHA256 algorithm.
      Parameters:
      key - the shared secret
      instant - current Unix time
      stepLength - length of time step
      digitsNumber - number of digits to return
      Returns:
      a numeric String in base 10 that includes truncation digits
      Throws:
      CryptoProviderException - in case of any crypto error
    • generateTotpSha512

      public static byte[] generateTotpSha512(byte[] key, Instant instant, int digitsNumber) throws CryptoProviderException
      Generates a TOTP value for the given set of parameters using HmacSHA512 algorithm.
      Parameters:
      key - the shared secret
      instant - current Unix time
      digitsNumber - number of digits to return
      Returns:
      a numeric String in base 10 that includes truncation digits
      Throws:
      CryptoProviderException - in case of any crypto error
    • generateTotpSha512

      public static byte[] generateTotpSha512(byte[] key, Instant instant, Duration stepLength, int digitsNumber) throws CryptoProviderException
      Generates a TOTP value for the given set of parameters using HmacSHA512 algorithm.
      Parameters:
      key - the shared secret
      instant - current Unix time
      stepLength - length of time step
      digitsNumber - number of digits to return
      Returns:
      a numeric String in base 10 that includes truncation digits
      Throws:
      CryptoProviderException - in case of any crypto error
    • validateTotpSha256

      public static boolean validateTotpSha256(byte[] otp, byte[] key, Instant instant, int digitsNumber) throws CryptoProviderException
      Validate a TOTP value for the given set of parameters using HmacSHA256 algorithm. Validates one time step backward.
      Parameters:
      otp - TOTP to validate
      key - the shared secret
      instant - current Unix time
      digitsNumber - expected length of the TOTP
      Returns:
      true if OTP is valid
      Throws:
      CryptoProviderException - in case of any crypto error
      See Also:
    • validateTotpSha256

      public static boolean validateTotpSha256(byte[] otp, byte[] key, Instant instant, int digitsNumber, int steps, Duration stepLength) throws CryptoProviderException
      Validate a TOTP value for the given set of parameters using HmacSHA256 algorithm.
      Parameters:
      otp - TOTP to validate
      key - the shared secret
      instant - current Unix time
      digitsNumber - expected length of the TOTP
      steps - number of backward time steps allowed to validate
      stepLength - length of time step
      Returns:
      true if OTP is valid
      Throws:
      CryptoProviderException - in case of any crypto error
    • validateTotpSha512

      public static boolean validateTotpSha512(byte[] otp, byte[] key, Instant instant, int digitsNumber) throws CryptoProviderException
      Validate a TOTP value for the given set of parameters using HmacSHA512 algorithm. Validates one time step backward.
      Parameters:
      otp - TOTP to validate
      key - the shared secret
      instant - current Unix time
      digitsNumber - expected length of the TOTP
      Returns:
      true if OTP is valid
      Throws:
      CryptoProviderException - in case of any crypto error
      See Also:
    • validateTotpSha512

      public static boolean validateTotpSha512(byte[] otp, byte[] key, Instant instant, int digitsNumber, int steps, Duration stepLength) throws CryptoProviderException
      Validate a TOTP value for the given set of parameters using HmacSHA512 algorithm.
      Parameters:
      otp - TOTP to validate
      key - the shared secret
      instant - current Unix time
      digitsNumber - expected length of the TOTP
      steps - number of backward time steps allowed to validate
      stepLength - length of time step
      Returns:
      true if OTP is valid
      Throws:
      CryptoProviderException - in case of any crypto error