org.keycloak.models.utils

## Class HmacOTP

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

- Direct Known Subclasses:
    TimeBasedOTP

      public class HmacOTP
      extends Object

  Version:
    $Revision: 1 $
  Author:
    Bill Burke

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected String` | `algorithm`
    `static String` | `DEFAULT_ALGORITHM`
    `static int` | `DEFAULT_NUMBER_DIGITS`
    `static String` | `HMAC_SHA1`
    `static String` | `HMAC_SHA256`
    `static String` | `HMAC_SHA512`
    `protected int` | `lookAheadWindow`
    `protected int` | `numberDigits`

  - ### Constructor Summary

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

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `generateHOTP(String key, int counter)`
    `String` | `generateOTP(String key, String counter, int returnDigits, String crypto)` This method generates an OTP value for the given set of parameters.
    `static String` | `generateSecret(int length)`
    `int` | `validateHOTP(String token, String key, int counter)`

    - ### 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

    - #### algorithm

          protected final String algorithm

    - #### numberDigits

          protected final int numberDigits

    - #### lookAheadWindow

          protected final int lookAheadWindow

  - ### Constructor Detail

    - #### HmacOTP

          public HmacOTP(int numberDigits,
                         String algorithm,
                         int delayWindow)

  - ### Method Detail

    - #### generateSecret

          public static String generateSecret(int length)

    - #### generateHOTP

          public String generateHOTP(String key,
                                     int counter)

    - #### validateHOTP

          public int validateHOTP(String token,
                                  String key,
                                  int counter)

      Parameters:
        `token` -
        `key` -
        `counter` -
      Returns:
        -1 if not a match. A positive number means successful validation. This positive number is also the new value of the counter

    - #### generateOTP

          public String generateOTP(String key,
                                    String counter,
                                    int returnDigits,
                                    String crypto)

      This method generates an OTP value for the given set of parameters.
      Parameters:
        `key` - the shared secret, HEX encoded
        `counter` - 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 return digits
      Throws:
        `GeneralSecurityException`

Copyright © 2021 JBoss by Red Hat. All rights reserved.