如何从 Lua 生成 websocket 握手?

发布于 2025-01-07 08:24:32 字数 201 浏览 1 评论 0原文

Lua中是否有现有的函数可以生成服务器响应密钥?这是Python中的解决方案:websocket握手问题

我确实捕获了两个关键数字,计算了空格,捕获第三个字符串并希望其余部分位于现有函数中......

Is there an existing function to generate the server response key in Lua? Here is the solution in python: websocket handshake problem

I do have the two key numbers captured, the spaces counted, the third string captured and hoping the rest lies in an existing function...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2025-01-14 08:24:32

如果需要旧的握手(协议 0),您可以使用以下代码从两个密钥获取握手值:

md5 = require 'md5'

function getnumbers(str)
    local num = ""
    str:gsub('%d', function(d) num = num .. d end)
    return tonumber(num)
end
function countspaces(str)
    return select(2, str:gsub(' ', ' '))
end
function to32bitint(i)
    return string.char(i/256^3 % 256, i/256^2 % 256, i/256 % 256, i % 256)
end
function websocketresponse(key1, key2, end8)
    local n1, s1 = getnumbers(key1), countspaces(key1)
    local n2, s2 = getnumbers(key2), countspaces(key2)
    local cat = to32bitint(n1/s1) .. to32bitint(n2/s2) .. ending8
    return md5.sum(cat)
end

websocket_key1 = "18x 6]8vM;54 *(5:  {   U1]8  z [  8"
websocket_key2 = "1_ tx7X d  <  nw  334J702) 7]o}` 0"
ending8 = "Tm[K T2u"
print(websocketresponse(websocket_key1, websocket_key2, ending8))
--> fQJ,fN/4F4!~K~MH

这会产生与 协议草案。此示例使用 MD5 库来计算校验和,并可在 LuaForWindows

WebSocket 协议版本 6 的实现要简单得多:

crypto = require 'crypto'
mime = require 'mime'

function websocketresponse6(key)
    local magic = key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    return (mime.b64(crypto.digest('sha1', magic, true)))
end 

key6 = "x3JJHMbDL1EzLkh9GBhXDw=="
print(websocketresponse6(key6))
--> HSmrc0sMlYUkAGmm5OPpG2HaGWk=

此示例使用 LuaCrypto 进行 SHA1 总和和 MIME

If need the older handshake (protocol 0), you can use the following code to get the handshake value from the two keys:

md5 = require 'md5'

function getnumbers(str)
    local num = ""
    str:gsub('%d', function(d) num = num .. d end)
    return tonumber(num)
end
function countspaces(str)
    return select(2, str:gsub(' ', ' '))
end
function to32bitint(i)
    return string.char(i/256^3 % 256, i/256^2 % 256, i/256 % 256, i % 256)
end
function websocketresponse(key1, key2, end8)
    local n1, s1 = getnumbers(key1), countspaces(key1)
    local n2, s2 = getnumbers(key2), countspaces(key2)
    local cat = to32bitint(n1/s1) .. to32bitint(n2/s2) .. ending8
    return md5.sum(cat)
end

websocket_key1 = "18x 6]8vM;54 *(5:  {   U1]8  z [  8"
websocket_key2 = "1_ tx7X d  <  nw  334J702) 7]o}` 0"
ending8 = "Tm[K T2u"
print(websocketresponse(websocket_key1, websocket_key2, ending8))
--> fQJ,fN/4F4!~K~MH

This produces the same value as the example given in the protocol draft. This example uses MD5 library to calculate the checksum and is available compiled in LuaForWindows.

The implementation for WebSocket protocol version 6 is much simpler:

crypto = require 'crypto'
mime = require 'mime'

function websocketresponse6(key)
    local magic = key .. "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    return (mime.b64(crypto.digest('sha1', magic, true)))
end 

key6 = "x3JJHMbDL1EzLkh9GBhXDw=="
print(websocketresponse6(key6))
--> HSmrc0sMlYUkAGmm5OPpG2HaGWk=

This example uses the LuaCrypto for SHA1 sum and MIME from LuaSocket.

人海汹涌 2025-01-14 08:24:32

Have a look at the lua-websockets implementation. Here is the sha1 stuff.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文