Вопрос

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...

Это было полезно?

Решение

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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top