org.keycloak.models.utils

## Class TimeBasedOTP

- java.lang.Object
- - org.keycloak.models.utils.TimeBasedOTP

-     public class TimeBasedOTP
      extends Object

  TOTP: Time-based One-time Password Algorithm Based on http://tools.ietf.org/html/draft-mraihi-totp-timebased-06
  Since:
    Sep 20, 2010
  Author:
    anil saldhana

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `DEFAULT_ALGORITHM`
    `static int` | `DEFAULT_DELAY_WINDOW`
    `static int` | `DEFAULT_INTERVAL_SECONDS`
    `static int` | `DEFAULT_NUMBER_DIGITS`
    `static String` | `HMAC_SHA1`
    `static String` | `HMAC_SHA256`
    `static String` | `HMAC_SHA512`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TimeBasedOTP()`
    `TimeBasedOTP(String algorithm, int numberDigits, int timeIntervalInSeconds, int delayWindow)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `generate(String secretKey)` Generates a token.
    `String` | `generateTOTP(String key, String time, int returnDigits, String crypto)` This method generates an TOTP value for the given set of parameters.
    `void` | `setCalendar(Calendar calendar)`
    `boolean` | `validate(String token, byte[] secret)` Validates a token using a secret key.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### HMAC_SHA1

          public static final String HMAC_SHA1

      See Also:
        Constant Field Values

    - #### HMAC_SHA256

          public static final String HMAC_SHA256

      See Also:
        Constant Field Values

    - #### HMAC_SHA512

          public static final String HMAC_SHA512

      See Also:
        Constant Field Values

    - #### DEFAULT_ALGORITHM

          public static final String DEFAULT_ALGORITHM

      See Also:
        Constant Field Values

    - #### DEFAULT_NUMBER_DIGITS

          public static final int DEFAULT_NUMBER_DIGITS

      See Also:
        Constant Field Values

    - #### DEFAULT_INTERVAL_SECONDS

          public static final int DEFAULT_INTERVAL_SECONDS

      See Also:
        Constant Field Values

    - #### DEFAULT_DELAY_WINDOW

          public static final int DEFAULT_DELAY_WINDOW

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### TimeBasedOTP

          public TimeBasedOTP()

    - #### TimeBasedOTP

          public TimeBasedOTP(String algorithm,
                      int numberDigits,
                      int timeIntervalInSeconds,
                      int delayWindow)

      Parameters:
        `algorithm` - the encryption algorithm
        `numberDigits` - the number of digits for tokens
        `timeIntervalInSeconds` - the number of seconds a token is valid
        `delayWindow` - the number of previous intervals that should be used to validate tokens.

  - ### Method Detail

    - #### generate

          public String generate(String secretKey)

      Generates a token.

      Parameters:
        `secretKey` - the secret key to derive the token from.

    - #### generateTOTP

          public String generateTOTP(String key,
                            String time,
                            int returnDigits,
                            String crypto)

      This method generates an TOTP value for the given set of parameters.
      Parameters:
        `key` - the shared secret, HEX encoded
        `time` - a value that reflects a time
        `returnDigits` - number of digits to return
        `crypto` - the crypto function to use
      Returns:
        A numeric String in base 10 that includes `truncationDigits` digits
      Throws:
        `GeneralSecurityException`

    - #### validate

          public boolean validate(String token,
                         byte[] secret)

      Validates a token using a secret key.

      Parameters:
        `token` - OTP string to validate
        `secret` - Shared secret
      Returns:

    - #### setCalendar

          public void setCalendar(Calendar calendar)

Copyright © 2014. All Rights Reserved.