使用 sha512 与使用 sha1 时,一次性密码算法是否有具体区别

Is there a specific difference in the one-time-password algorithm when using sha512 to using sha1

我正在编写一个 pam 模块,它使用一次性密码进行身份验证。 我开始用 sha512 作为 hmac 的算法编写它,运行 变成了一个问题。我也用 sha1 做到了,这很有效。 代码位于:https://github.com/Ongy/pam_totp

我用 sha512 测试了 hmac,据我所知这是正确的,所以错误应该在 get_truncate 函数中,它对 sha1 有效,但对 sha512 无效。

static int get_truncate(const uint8_t * hash, size_t len, char * buffer,
        size_t maxlen)
{
    uint32_t value;
    uint8_t offset;
    offset = hash[len-1] & 0x0F;
    value = *((uint32_t *) (hash+offset));
    value = be32toh(value) & 0x7FFFFFFF;
    value %= 100000000;

    return snprintf(buffer, maxlen, "%08d", value);
}

中调用
static int get_totp_sha512(const uint8_t * hashdata, size_t len, uint64_t time,
            char * dst, size_t maxlen)
{
    uint8_t buffer[64];
    uint64_t counter = htobe64(time);

    memset(buffer, 0, sizeof(buffer));

    calculate_hmac_sha512(hashdata, len, (uint8_t *) &counter,
                     sizeof(counter), buffer, sizeof(buffer));

    return get_truncate(buffer, sizeof(buffer), dst, maxlen);
}

大部分相关代码应该在src/main.c.

相关的 rfcs 是:6238 and 4226。 (第二个描述了通用算法,第一个也允许除sha1之外的其他哈希)

好的,所以我必须修复的是我的测试用例,我使用了 https://www.rfc-editor.org/rfc/rfc6238#appendix-B 中给出的测试向量。我必须更改的内容:

密钥实际上不是 sha512 的 "12345678901234567890",而是 "1234567890123456789012345678901234567890123456789012345678901234"