Class HOTP

java.lang.Object
org.miaixz.bus.crypto.center.HOTP
Direct Known Subclasses:
TOTP

public class HOTP extends Object
HMAC-based one-time passwords (HOTP) 基于HMAC算法一次性密码生成器, 规范见:RFC 4226

基于事件同步,通过某一特定的事件次序及相同的种子值作为输入,通过HASH算法运算出一致的密码。

参考:https://github.com/jchambers/java-otp

Since:
Java 17+
Author:
Kimi Liu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    默认密码长度.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HOTP(byte[] key)
    构造,使用默认密码长度和默认HMAC算法(HmacSHA1)
    HOTP(int passwordLength, byte[] key)
    构造,使用默认HMAC算法(HmacSHA1)
    HOTP(int passwordLength, org.miaixz.bus.core.lang.Algorithm algorithm, byte[] key)
    构造
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    generate(long counter)
    生成一次性密码
    static String
    generateSecretKey(int numBytes)
    生成共享密钥的Base32表示形式
    获取HMAC算法
    int
    获取密码长度,可以是6,7,8

    Methods inherited from class java.lang.Object

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

    • DEFAULT_PASSWORD_LENGTH

      public static final int DEFAULT_PASSWORD_LENGTH
      默认密码长度.
      See Also:
  • Constructor Details

    • HOTP

      public HOTP(byte[] key)
      构造,使用默认密码长度和默认HMAC算法(HmacSHA1)
      Parameters:
      key - 共享密码,RFC 4226要求最少128位
    • HOTP

      public HOTP(int passwordLength, byte[] key)
      构造,使用默认HMAC算法(HmacSHA1)
      Parameters:
      passwordLength - 密码长度,可以是6,7,8
      key - 共享密码,RFC 4226要求最少128位
    • HOTP

      public HOTP(int passwordLength, org.miaixz.bus.core.lang.Algorithm algorithm, byte[] key)
      构造
      Parameters:
      passwordLength - 密码长度,可以是6,7,8
      algorithm - HMAC算法枚举
      key - 共享密码,RFC 4226要求最少128位
  • Method Details

    • generateSecretKey

      public static String generateSecretKey(int numBytes)
      生成共享密钥的Base32表示形式
      Parameters:
      numBytes - 将生成的种子字节数量。
      Returns:
      共享密钥
    • generate

      public int generate(long counter)
      生成一次性密码
      Parameters:
      counter - 事件计数的值,8 字节的整数,称为移动因子(moving factor), 可以是基于计次的动移动因子,也可以是计时移动因子
      Returns:
      一次性密码的int值
    • getPasswordLength

      public int getPasswordLength()
      获取密码长度,可以是6,7,8
      Returns:
      密码长度,可以是6,7,8
    • getAlgorithm

      public String getAlgorithm()
      获取HMAC算法
      Returns:
      HMAC算法