Answer the question
In order to leave comments, you need to log in
How to port the hash calculation algorithm to lua?
There is an algorithm in C:
extern uint32 HashRot13(const uint8 * str, int length)
{
uint32 hash = 0;
int i;
for(i = 0; i < length; ++i, str++)
{
hash += (uint32)(*str);
hash -= (hash << 13) | (hash >> 19);
}
return hash;
}
Answer the question
In order to leave comments, you need to log in
function HashRot13(bytes)
hash = 0
for i = 1, #bytes do
hash = hash + bytes[i]
hash = bit.band(hash - bit.rol(hash, 13), 0xFFFFFFFF)
end
return bit.bswap(hash )
end
Learn C, learn Lua. Then take and translate into one language into another.
hash += (uint32)(*str);
takes a uint32 number at the current *str pointer and accumulates it in the hash variable.
perform left shift by 13 bits (multiply hash by 2^13)
perform right shift by 19 bits (divide hash by 2^19)
Bitwise OR from result of shifts above
update with subtraction from accumulated hash value
You can take and rewrite the code from C to Lua. The trouble is that bit operations in lua out of the box suck. But there is this library (although it seems to be only for luajit).
The second and better option is to use the C++ library. Lua integrates very well with sish. If you take the usual implementation, you can write a sish module for lua. If luajit - then there is FFI out of the box , it's enough to pull the C functions.
Or at least tell us what it does.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question