2

I need to convert a sip callId (eg. 1097074724_100640573@8.8,8.8) string into requestId and I am using sha1 digest to get a hash. I need to convert this hex-decimal into uint64_t due to internal compatibility:

--
-- Obtain request-id from callId
-- 
--  Returns hash
--
function common_get_request_id( callId )
   local command = "echo -n \"" .. callId .. "\" | openssl sha1 | sed 's/(stdin)= //g'"
   local handle = assert( io.popen( command, "r" ) )
   local output = handle:read( "*all" )
   local outputHash = string.gsub(output, "\n", "") -- strip newline
   handle:close()

   -- How to convert outputHash to uint64?  
end

I am not sure about uint64 support in Lua. Also, how to do the conversion?

ssk
  • 9,045
  • 26
  • 96
  • 169

1 Answers1

0

You can receive two 32-bit integer numbers from Lua as two "double" values and convert them to one "uint64" on C side.

outputHash = '317acf63c685455cfaaf1c3255eeefd6ca3c5571'

local p = 4241942993 -- some prime below 2^32
local c1, c2 = 0, 0

for a, b in outputHash:gmatch"(%x)(%x)" do
   c1 = (c1 * 16 + tonumber(a, 16)) % p
   c2 = (c2 * 16 + tonumber(b, 16)) % p
end

return c1, c2  -- both numbers 0..(2^32-1)
Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23