php: number only hash?

后端 未结 7 1989
眼角桃花
眼角桃花 2020-12-03 01:00

In php is there a way to give a unique hash from a string, but that the hash was made up from numbers only?

example:

return md5(234); // returns 098f         


        
7条回答
  •  清歌不尽
    2020-12-03 01:24

    The problem of cut off the hash are the collisions, to avoid it try:

    return  hexdec(crc32("Hello World"));
    

    The crc32():

    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.

    That give us an integer of 32 bit, negative in 32 bits installation, or positive in the 64 bits. This integer could be store like an ID in a database. This don´t have collision problems, because it fits into 32bits variable, once you convert it to decimal with the hexdec() function.

提交回复
热议问题