I am writing a mechanism to generate a unique ID derived from sha256. I use:
require 'sha2'
function getUint64ID( callId )
local minimumValue = 100000000000000000
local maximumValue = 999999999999999999
outputHash = sha2.sha256hex(callId)
print(outputHash .. "\n")
local c1 = ""
for a, b in outputHash:gmatch"(%x)(%x)" do
hexTuple = tostring(a) .. tostring(b)
intVal = tonumber(hexTuple, 16)
c1 = c1 .. string.char(intVal)
end
uint64ID = ( minimumValue + ( tonumber(c1) % ( maximumValue - minimumValue + 1 ) ) )
print('uint64ID:')
print(string.format("%18.0f",uint64ID))
end
getUint64ID("test")
I get the following error in the above code:
stdin:17: attempt to perform arithmetic on a nil value
stack traceback:
stdin:17: in function 'getUint64ID'
stdin:1: in main chunk
[C]: ?
How to convert 32 bytes char buffer (c1) into a uint64 number in lua?