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