在mysql中实现rfc4226(HOTP)的部分内容
就像标题所说,我正在尝试用 SQL 实现 RFC4226“HOTP:基于 HMAC 的一次性密码算法”的编程部分。我想我已经有了一个可以工作的版本(对于一个小的测试样本,它产生与代码中的Java版本相同的结果),但它包含一对嵌套的 hex(unhex()) 调用,我感觉还可以做得更好。我受到以下限制:a)需要执行此算法,b)需要在 mysql 中执行此操作,否则我很乐意查看其他执行此操作的方法。
到目前为止我所得到的:
-- From the inside out...
-- Concatinate the users secret, and the number of time its been used
-- find the SHA1 hash of that string
-- Turn a 40 byte hex encoding into a 20 byte binary string
-- keep the first 4 bytes
-- turn those back into a hex represnetation
-- convert that into an integer
-- Throw away the most-significant bit (solves signed/unsigned problems)
-- Truncate to 6 digits
-- store into otp
-- from the otpsecrets table
select (conv(hex(substr(unhex(sha1(concat(secret, uses))), 1, 4)), 16, 10) & 0x7fffffff) % 1000000
into otp
from otpsecrets;
是否有更好(更有效)的方法来做到这一点?
Like the title says, I'm trying to implement the programmatic parts of RFC4226 "HOTP: An HMAC-Based One-Time Password Algorithm" in SQL. I think I've got a version that works (in that for a small test sample, it produces the same result as the Java version in the code), but it contains a nested pair of hex(unhex()) calls, which I feel can be done better. I am constrained by a) needing to do this algorithm, and b) needing to do it in mysql, otherwise I'm happy to look at other ways of doing this.
What I've got so far:
-- From the inside out...
-- Concatinate the users secret, and the number of time its been used
-- find the SHA1 hash of that string
-- Turn a 40 byte hex encoding into a 20 byte binary string
-- keep the first 4 bytes
-- turn those back into a hex represnetation
-- convert that into an integer
-- Throw away the most-significant bit (solves signed/unsigned problems)
-- Truncate to 6 digits
-- store into otp
-- from the otpsecrets table
select (conv(hex(substr(unhex(sha1(concat(secret, uses))), 1, 4)), 16, 10) & 0x7fffffff) % 1000000
into otp
from otpsecrets;
Is there a better (more efficient) way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有阅读规范,但我认为您不需要在十六进制和二进制之间来回转换,因此这可能会更有效一些:
这似乎给出了与我的几个示例查询相同的结果已测试。
I haven't read the spec, but I think you don't need to convert back and forth between hex and binary, so this might be a little more efficient:
This seems to give the same result as your query for a few examples I tested.
这绝对是可怕的,但它适用于我的 6 位 OTP 令牌。调用方式为:
This is absolutely horrific, but it works with my 6-digit OTP tokens. Call as: