Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP hashing function that returns an integer (32bit int)

The goal is to store the hash on a mysql database, using INT (not BIGINT or MEDIUMINT). md5('string', true) returns binary data, 16 bytes of hash. I thought i could grep the first 4 bytes and convert it to an INT(32bit/4bytes) integer, but i don't know how to do it.

What do you suggest? Thanks.

like image 216
cedivad Avatar asked Dec 15 '11 13:12

cedivad


1 Answers

Use crc32, it will return a 32bit int.


var_dump (crc32 ("hello world"));
var_dump (crc32 ("world hello"));

output

int(222957957)
int(1292159901)

PHP: crc32 - Manual

Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the str. This is usually used to validate the integrity of data being transmitted.

Because PHP's integer type is signed, and many crc32 checksums will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned crc32 checksum.

like image 131
Filip Roséen - refp Avatar answered Sep 20 '22 13:09

Filip Roséen - refp