Posted 12 February 2012 - 10:08 PM
i am trying to make a set of hashing and encryption libraries for this mod however i am not that skilled in lua, without a line number i do not see who i am getting this error, the code is attached
i am trying to adapt this from this library that relies on the C implementation of lua with the bit library.
rom/apis/md5
rom/apis/ble
i am trying to adapt this from this library that relies on the C implementation of lua with the bit library.
rom/apis/md5
Spoiler
ff=tonumber("ffffff",16)
t={}
for i=1,64,1 do
t[i]=(4294967296*math.abs(math.sin(i)))
end
local a,b,c,d
local function f(x,y,z)
return bit.bor(bit.band(x,y),bit.band(-x-1,z))
end
local function g(x,y,z)
return bit.bor(bit.band(x,z),bit.band(y,-z-1))
end
local function h(x,y,z)
return bit.bxor(x,bit.bxor(y,z))
end
local function i(x,y,z)
return bit.bxor(y,but.bor(x,-z-1))
end
local function z(f,a,b,c,d,x,s,ac)
a=bit.band(a+f(b,c,d)+x+ac,ff)
-- be *very* careful that left shift does not cause rounding!
return bit.bor(bit.lshift(bit.band(a,bit.rshift(ff,s)),s),bit.rshift(a,32-s))+b
end
local function pad(s)
msgLen=strlen(s)
local padLen=56-imod(msgLen,64)
if imod(msgLen,64)>56 then padLen=padLen+64 end
if padLen==0 then padLen=64 end
s=s..strchar(128)..strrep(strchar('0'),padLen-1)
s=s..leIstr(8*msgLen)..leIstr('0')
return s
end
local function initializeBuffers()
local a=tonumber("10234567",16)
local b=tonumber("89abcdef",16)
local c=tonumber("fedcba98",16)
local d=tonumber("76543210",16)
end
local function transform(a,b,c,d)
local a,b,c,d=A,B,C,D
a=z(f,a,b,c,d,X[ 0], 7,t[ 1])
d=z(f,d,a,b,c,X[ 1],12,t[ 2])
c=z(f,c,d,a,b,X[ 2],17,t[ 3])
b=z(f,b,c,d,a,X[ 3],22,t[ 4])
a=z(f,a,b,c,d,X[ 4], 7,t[ 5])
d=z(f,d,a,b,c,X[ 5],12,t[ 6])
c=z(f,c,d,a,b,X[ 6],17,t[ 7])
b=z(f,b,c,d,a,X[ 7],22,t[ 8])
a=z(f,a,b,c,d,X[ 8], 7,t[ 9])
d=z(f,d,a,b,c,X[ 9],12,t[10])
c=z(f,c,d,a,b,X[10],17,t[11])
b=z(f,b,c,d,a,X[11],22,t[12])
a=z(f,a,b,c,d,X[12], 7,t[13])
d=z(f,d,a,b,c,X[13],12,t[14])
c=z(f,c,d,a,b,X[14],17,t[15])
b=z(f,b,c,d,a,X[15],22,t[16])
a=z(g,a,b,c,d,X[ 1], 5,t[17])
d=z(g,d,a,b,c,X[ 6], 9,t[18])
c=z(g,c,d,a,b,X[11],14,t[19])
b=z(g,b,c,d,a,X[ 0],20,t[20])
a=z(g,a,b,c,d,X[ 5], 5,t[21])
d=z(g,d,a,b,c,X[10], 9,t[22])
c=z(g,c,d,a,b,X[15],14,t[23])
b=z(g,b,c,d,a,X[ 4],20,t[24])
a=z(g,a,b,c,d,X[ 9], 5,t[25])
d=z(g,d,a,b,c,X[14], 9,t[26])
c=z(g,c,d,a,b,X[ 3],14,t[27])
b=z(g,b,c,d,a,X[ 8],20,t[28])
a=z(g,a,b,c,d,X[13], 5,t[29])
d=z(g,d,a,b,c,X[ 2], 9,t[30])
c=z(g,c,d,a,b,X[ 7],14,t[31])
b=z(g,b,c,d,a,X[12],20,t[32])
a=z(h,a,b,c,d,X[ 5], 4,t[33])
d=z(h,d,a,b,c,X[ 8],11,t[34])
c=z(h,c,d,a,b,X[11],16,t[35])
b=z(h,b,c,d,a,X[14],23,t[36])
a=z(h,a,b,c,d,X[ 1], 4,t[37])
d=z(h,d,a,b,c,X[ 4],11,t[38])
c=z(h,c,d,a,b,X[ 7],16,t[39])
b=z(h,b,c,d,a,X[10],23,t[40])
a=z(h,a,b,c,d,X[13], 4,t[41])
d=z(h,d,a,b,c,X[ 0],11,t[42])
c=z(h,c,d,a,b,X[ 3],16,t[43])
b=z(h,b,c,d,a,X[ 6],23,t[44])
a=z(h,a,b,c,d,X[ 9], 4,t[45])
d=z(h,d,a,b,c,X[12],11,t[46])
c=z(h,c,d,a,b,X[15],16,t[47])
b=z(h,b,c,d,a,X[ 2],23,t[48])
a=z(i,a,b,c,d,X[ 0], 6,t[49])
d=z(i,d,a,b,c,X[ 7],10,t[50])
c=z(i,c,d,a,b,X[14],15,t[51])
b=z(i,b,c,d,a,X[ 5],21,t[52])
a=z(i,a,b,c,d,X[12], 6,t[53])
d=z(i,d,a,b,c,X[ 3],10,t[54])
c=z(i,c,d,a,b,X[10],15,t[55])
b=z(i,b,c,d,a,X[ 1],21,t[56])
a=z(i,a,b,c,d,X[ 8], 6,t[57])
d=z(i,d,a,b,c,X[15],10,t[58])
c=z(i,c,d,a,b,X[ 6],15,t[59])
b=z(i,b,c,d,a,X[13],21,t[60])
a=z(i,a,b,c,d,X[ 4], 6,t[61])
d=z(i,d,a,b,c,X[11],10,t[62])
c=z(i,c,d,a,b,X[ 2],15,t[63])
b=z(i,b,c,d,a,X[ 9],21,t[64])
return A+a,B+b,C+c,D+d
end
function calc(s)
pad(s)
initializeBuffers()
for i=0,strlen(s),64 do
X=leStrCuts(strsub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
X[0]=tremove(X,1) -- zero based!
end
abcd=transform(a,b,c,d)
local swap=function (w) return beInt(leIstr(w)) end
return format("%08x%08x%08x%08x",swap(a),swap(;)/>/>,swap(c),swap(d))
end
md5.calc=calc
Spoiler
-- convert little-endian 32-bit int to a 4-char string
function leIstr(i)
local f=function (s) return strchar(bit.band(bit.rshift(i,s),255)) end
return f(0)..f(8)..f(16)..f(24)
end
do -- from util.lua
-- convert raw string to big-endian int
function beInt(s)
local v=0
for i=1,strlen(s) do v=v*256+strbyte(s,i) end
return v
end
-- convert raw string to little-endian int
function leInt(s)
local v=0
for i=strlen(s),1,-1 do v=v*256+strbyte(s,i) end
return v
end
-- cut up a string in little-endian ints of given size
function leStrCuts(s,...)
local o,r=1,{}
for i=1,getn(arg) do
tinsert(r,leInt(strsub(s,o,o+arg[i]-1)))
o=o+arg[i]
end
return r
end
end