django-otp:生成 hotp 令牌时我应该增加计数器吗?

django-otp: should I increment the counter when generating hotp token?

我使用django-otp生成hotp, 我还没有找到正确生成 hotp 令牌的文档

所以我阅读了 HOTPDevice 模型 class 中的 verify_token 方法,找到了如何验证令牌

if hotp(key, counter, self.digits) == token:
    verified = True
    self.counter = counter + 1
    self.save()

从上面我知道我可以使用 hotp 函数创建令牌。

然后我使用 hotp 函数生成令牌,但我注意到它在未确认时反复 returns 相同的令牌。那么我应该在生成它时也增加 counter 吗?

来源:HOTPDevice source from django-otp

所以我找到了 RFC 4226 - HOTP,

的引用

The HOTP client (hardware or software token) increments its counter and then calculates the next HOTP value HOTP client

所以我应该先增加counter,将其保存到对象中,然后returns生成的令牌