lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Up to now, I was only able to change the inner for-loop to use a table.concat instead of string concat operator "..".
But, I'm still not happy with it...

        local decrypted_t = {}
local number_parts = #data / RSA_LENGTH_HEX
for i= 1, number_parts do
local part = string.sub(data, 1, RSA_LENGTH_HEX)
local dec_part = rsa.decrypt_hex(part, priv_exp, priv_modulus)
local F_pos = string.find(dec_part, "F")
decrypted_t[#decrypted_t+1]=string.sub(dec_part, F_pos+1)
data = "" RSA_LENGTH_HEX + 1)
end
local decrypted=table.concat(decrypted_t)




On Wed, Nov 10, 2010 at 4:06 AM, Valerio Schiavoni <valerio.schiavoni@gmail.com> wrote:
Hello,
i have to reduce the memory consumption of the function function. 
Are there *obvious* things you see in there ? As as possible, i will try to port this code to some other library, but in the meanwhile I really would like some suggestions by expert lua develpers.

it's ugly, I know.

-[[
Decrypt the data with the specified private key. 
If the decoded nexthop is "000000000000", the function
returns the plaintext message.
If not, the function returns the triplet data,ip_nexthop,port_nexthop. 
]]
function onion_decrypt(key,data)
--write key to file
local privk_file = "/tmp/"..crypto.evp.new("sha1"):digest(math.random().."temp_file") --os.tmpname()
local out = assert(io.open(privk_file, "w"))
out:write(key)
  out:close()
local priv_exp = rsa.initPrivExp(privk_file)
local priv_modulus = rsa.initModulus(privk_file)
local decrypted = ""
local number_parts = #data / RSA_LENGTH_HEX
for i= 1, number_parts do
part = string.sub(data, 1, RSA_LENGTH_HEX)
local dec_part = rsa.decrypt_hex(part, priv_exp, priv_modulus)
local F_pos = string.find(dec_part, "F")
decrypted = decrypted..string.sub(dec_part, F_pos+1)
data = "" RSA_LENGTH_HEX + 1)
end
local data = "" 1, #decrypted-12)
local nexthop_hex = string.sub(decrypted, #decrypted-11)
if nexthop_hex == LAST_HOP then
return rsa.hex2string(data)
else 
local nexthop_ip, nexthop_port = hex2ip_port(nexthop_hex)
return data, nexthop_ip, nexthop_port
end
end


Thank you,
Valerio