Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRC32 integer hash to string

Tags:

hash

lua

crc32

I was looking for a Lua implementation of CRC32 and stumbled upon this: https://github.com/openresty/lua-nginx-module/blob/master/t/lib/CRC32.lua

However it returns the integer hash, how would I go about getting the string equivalent of it?

Using the input "something" it returns: 1850105976

Using an online CRC32 generator I get: "879fb991"

like image 599
user1826176 Avatar asked Nov 21 '25 08:11

user1826176


1 Answers

There are many CRC-32 algorithms. You can find ten different CRC-32s documented in this catalog. The Lua code you found and the online CRC32 you found (somewhere -- no link was provided) are different CRC-32s.

What you seem to mean by a "string equivalent" is the hexadecimal representation of the 32-bit integer. In Lua you can use string.format with the print format %x to get hexadecimal. For the example you gave, 1850105976, that would be 6e466078.

Your "online CRC32 generator" appears to be using the BZIP2 CRC-32, though it is showing you the bytes of the resulting CRC in reversed order (little-endian). So the actual CRC in that case in hexadecimal is 91b99f87. The Lua code you found appears to be using the MPEG-2 CRC-32. The only difference between those is the exclusive-or with ffffffff. So in fact the exclusive-or of the two CRCs you got from the two different sources, 6e466078 ^ 91b99f87 is ffffffff.

like image 112
Mark Adler Avatar answered Nov 23 '25 00:11

Mark Adler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!