java - HOTP - 正确使用 RFC 示例

标签 java

我目前正在尝试实现一个使用 RFC4226 - HOTP: An HMAC-Based One-Time Password Algorithm 作为基础的示例。

我获取了代码示例并添加了:

public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {
    // Seed
    String secret = "12345678901234567890";
    byte[] secretBytes = secret.getBytes();

    int counter;
    for (counter = 0; counter < 9; counter++) {
        String strGeneratedToken = OneTimePasswordAlgorithm.generateOTP(secretBytes, counter, 6, false, 0);
        System.out.println(strGeneratedToken);
    }
}

我得到的是:

755224 717529 868666 023335 179456 490877 910469 467724 952310

第一个没问题,但根据 RFC (755224 287082 359152 969429 338314 254676 287922 162583 399871 520489),下一个 (counter=1)。

我已将代码上传到 GitHub https://github.com/n0l0cale/hotp - 也许有人能够看到该问题。

这个实现似乎有同样的问题: http://read.pudn.com/downloads158/sourcecode/others/706340/MessageAuthenticationExample.java__.htm

我不会介意,但是当我为“我的 Java 应用程序”和“我的 Google Authenticator App”尝试相同的 secret 时,我还得到了其他代码。 “12345678901234567890”当然不能作为 secret ,但我尝试过使用相同的密码,Google Authenticator 应用程序似乎从 0 开始计数器,但在第一次使用时增加计数器......

最佳答案

truncationOffset好像需要设置为16......

String strGenerateToken = OneTimePasswordAlgorithm.generateOTP(secretBytes, counter, 6, false, 16);

public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException {
    // Seed
    String secret = "12345678901234567890";
    byte[] secretBytes = secret.getBytes();

    int counter;
    for (counter = 0; counter < 9; counter++) {
        String strGeneratedToken = OneTimePasswordAlgorithm.generateOTP(secretBytes, counter, 6, false, 16);
        System.out.println(strGeneratedToken);
    }
}

755224 287082 359152 969429 338314 254676 287922 162583 399871 520489

关于java - HOTP - 正确使用 RFC 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50923576/

相关文章:

java - 如何从给定的字符串中删除子字符串?

java - Android RecyclerView 和 onBindViewHolder

java - 当提供以 HTML 作为值的 JSON 输入时,REST API 返回 400(错误请求)

java - 避免 Maven 配置文件生成重复的 jar

java - 为什么 java.util.Arrays.sort(Object[]) 使用 2 种排序算法?

java - JRip.RipperRule.toString(classAttr) 给出错误的结果

java - 保存图像的正确位置(spring/jelastic)

java - GUI 没有出现在 IntelliJ IDEA 中

java - java中的SQL返回错误的数据

java - (a != b) 和 (a != (a = b) 有什么区别?