PtokaX forum

Archive => Archived 5.0 boards => Request for scripts => Topic started by: Meka][Meka on 12 September, 2005, 15:43:08

Title: Lock2Key Lua 5
Post by: Meka][Meka on 12 September, 2005, 15:43:08
any chance of a lock2key lua, maybe even using some lib, i've tried to write, but have problems with xor....

language samples: http://66.249.93.104/search?q=cache:mz5rusF3zw8J:dcplusplus.sourceforge.net/wiki/index.php/LockToKey%3Fversion%3D25+dc+locktokey&hl=da (http://66.249.93.104/search?q=cache:mz5rusF3zw8J:dcplusplus.sourceforge.net/wiki/index.php/LockToKey%3Fversion%3D25+dc+locktokey&hl=da)

thanks in advance...
Title:
Post by: Corayzon on 12 September, 2005, 15:50:23
What are u sceeming upto now? ^^

Nuh sorry mate, i checked around and tried a few things, none of them worked.

Its prob not the algorithm, but lua's encoding ;) U want to be using utf7 or 8, cant remember which one
Title:
Post by: Mardeg on 13 September, 2005, 20:09:22
try this:
-- Coded by Mardeg
function bODD(x)
       return x ~= math.floor(x / 2) * 2
end

function bitwise(x, y, bw)
       local c, p = 0, 1
       while x > 0 or y > 0 do
           if bw == "xor" then
               if (bODD(x) and not bODD(y)) or (bODD(y) and not bODD(x)) then
                       c = c + p
               end
           elseif bw == "and" then
               if bODD(x) and bODD(y) then
                       c = c + p
               end
           elseif bw == "or" then
               if bODD(x) or bODD(y) then
                       c = c + p
               end
           end
           x = math.floor(x / 2)
           y = math.floor(y / 2)
           p = p * 2
       end
       return c
end

function nibbleswap(bits)
  return bitwise(bitwise(bits*(2^4),240,"and"),bitwise(math.floor(bits/(2^4)),15,"and"),"or")
end

function lock2key(lock)
local key = {}
table.insert(key,bitwise(bitwise(bitwise(string.byte(lock,1),string.byte(lock,-1),"xor"),string.byte(lock,-2),"xor"),5,"xor"))
for i=2,string.len(lock),1 do
table.insert(key,bitwise(string.byte(lock,i),string.byte(lock,i - 1),"xor"))
end
local g = {["5"]=1,["0"]=1,["36"]=1,["96"]=1,["124"]=1,["126"]=1}
for i=1,table.getn(key),1 do
local b = nibbleswap(rawget(key,i))
rawset(key,i,(g[tostring(b)] and string.format("/%%DCN%03d%%/",b) or string.char(b)))
end
return table.concat(key)
end
Title:
Post by: Meka][Meka on 05 October, 2005, 11:21:09
well finally found my way back to the forum, seems it didnt, work but very nice try, i'll see if i can get it working, i actually think its my fault, a socket prob.....

Thanks!  :]
Title:
Post by: Mardeg on 06 October, 2005, 11:45:51
No problem, although I just did it as an exercise to compare different languages doing the same thing, so never tested the Lua version.

Specific things that may be worth checking are whether:
? arithmetic is allowable inside string.byte(lock,i - 1)
? string.format("/%%DCN%03d%%/",b) is correct
? each table.insert really does add each value to the end of the table
? the code can be optimised down in size without help from plugins or losing readability (this is just so it looks less daunting on the code comparison page: http://wiki.dcpp.net/index.php/LockToKey#Lua_5.0.2B - especially when I see how compact I made the Javascript example, still waiting for someone to make a Firefox extension to test that one.)
Title:
Post by: bastya_elvtars on 06 October, 2005, 12:33:17
QuoteOriginally posted by Mardeg
? arithmetic is allowable inside string.byte(lock,i - 1)
It is.

QuoteOriginally posted by Mardeg
each table.insert really does add each value to the end of the table
It does, unless you explicitly specify the position.
Title:
Post by: Mardeg on 06 October, 2005, 23:09:25
I've noticed that ptokax simply drops the connection without feedback if $Supports is not sent before $Key if that's any help.