使用纯Lua实现HMAC-MD5算法。
2017-5-23 12:4:34
收藏:0
阅读:152
评论:1
我需要在纯 Lua 中编写一个 HMAC-MD5算法。。
我从 维基百科 得到了这个算法
function hmac (key, message)
if (length(key) > blocksize) then
key = hash(key) // keys longer than blocksize are shortened
end if
if (length(key) < blocksize) then
key = key ∥ [0x00 * (blocksize - length(key))] // keys shorter than blocksize are zero-padded ('∥' is concatenation)
end if
o_key_pad = [0x5c * blocksize] ⊕ key // Where blocksize is that of the underlying hash function
i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR)
return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) // Where '∥' is concatenation
end function
我已经从 这里 得到了 md5 代码。md5 计算功能正确。
在 Lua 中执行该算法,我的代码如下:
local function hmac_md5(key,msg)
local blocksize = 64
if string.len(key) > blocksize then
key = calculateMD5(key)
end
while string.len(key)<blocksize do
key = key .. "0"
end
-- local o_key_pad = bit_xor((0x5c * blocksize),key)
-- local i_key_pad = bit_xor((0x36 * blocksize),key)
return calculateMD5(o_key_pad..calculateMD5(i_key_pad..message))
end
--calculateMD5 是 Stackoverflow 链接中的 md5.Calc 函数。
我卡在了计算 o_key_pad 和 i_key_pad 的部分上。我只需要对这两个值进行异或运算吗?维基百科链接中的 Python 实现有一些奇怪的计算,请帮帮我!
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
是的,"⊕" 是“异或”符号。
记住:一旦计算最终哈希,请勿使用普通字符串比较来检查哈希是否正确。这将 允许攻击者签署任意消息。
请注意,
0x5c * blocksize可能不是你要查找的,因为它将0x5c乘以blocksize。你想创建一个长度为blocksize的数组,每个位置都包含0x5c。请注意,你必须用零字节填充,而不是字符
"0"。所以key = key .. "0"是错误的。它应该是key = key .. "\0",或者在Lua中如何创建NUL字节。