2

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

Community
  • 1
  • 1

2 Answers2

1

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.

Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
  • I am using Lua within a Windows application. The distribution does not include crypto (or the % math operator, but math.mod works so that part is resolved). Is there a work around for not having crypto? Safari and Mobile Safari are both using the older protocol 0. –  Feb 19 '12 at 16:52
  • I do not know which Lua distribution you are using, but I guess that it is [Lua For Windows](http://code.google.com/p/luaforwindows/). It contains a [MD5](http://www.keplerproject.org/md5/manual.html#reference) library compiled, I will update the code for it. – Michal Kottman Feb 19 '12 at 17:04
  • I don't have the md5 library either. I am not sure how to add it with the interpreter only available within the application. (I've requested the developer to add either library.) I can fix these (only because I understand this portion), but neither **str:gsub('%d', function(d) num = num .. d end)** nor **select(2, str:gsub(' ', ' '))** will execute within the application... –  Feb 19 '12 at 18:11
  • What version of Lua are you using? This should work in Lua 5.1, which is around since early 2006. In the meantime, you can try this: `string.gsub(str, '%d', ...)` and `local _,n = string.gsub(str, ' ', ' '); return n` – Michal Kottman Feb 19 '12 at 19:21
  • Then use the code I provided (I just changed `str:gsub(...` to `string.gsub(str,...` and replaced the `select` call with equivalent Lua code). If you do not have a module for MD5 and cannot compile libraries, you can find pure Lua MD5 implementations [in this answer](http://stackoverflow.com/questions/6083262/pure-lua-implementation-of-md5). – Michal Kottman Feb 19 '12 at 19:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7926/discussion-between-shaun5-and-michal-kottman) –  Feb 19 '12 at 21:36
0

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

lipp
  • 5,586
  • 1
  • 21
  • 32