在Lua中是否有一個現有的生成服務器響應密鑰的函數?這裏是蟒蛇的解決方案:websocket handshake problem如何從Lua生成websocket握手?
我有兩個關鍵數字捕獲,空間計算,第三串捕獲並希望其餘的就在於在現有的功能...
在Lua中是否有一個現有的生成服務器響應密鑰的函數?這裏是蟒蛇的解決方案:websocket handshake problem如何從Lua生成websocket握手?
我有兩個關鍵數字捕獲,空間計算,第三串捕獲並希望其餘的就在於在現有的功能...
如果需要舊的握手(協議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
這產生了相同的值the protocol draft給出的例子。本示例使用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=
我在Windows應用程序中使用Lua。該分佈不包括加密(或%數學運算符,但math.mod可以工作,以便解決部分問題)。有沒有加密的工作? Safari和Mobile Safari都使用舊協議0. – shaun5 2012-02-19 16:52:57
我不知道你正在使用哪個Lua發行版,但我想它是[Lua For Windows](http://code.google.com/p/luaforwindows/) )。它包含編譯的[MD5](http://www.keplerproject.org/md5/manual.html#reference)庫,我將更新它的代碼。 – 2012-02-19 17:04:39
我沒有md5庫。我不確定如何將其與僅在應用程序中可用的解釋器一起添加。 (我已經要求開發人員添加任何一個庫)。我可以修復這些(僅因爲我理解這部分),但都不是** str:gsub('%d',function(d)num = num .. d end )**和** select(2,str:gsub('',''))**將在應用程序中執行... – shaun5 2012-02-19 18:11:01