md5-php How to hash a string into 10 binary digits

Source: Internet
Author: User
Tags crc32
MD5 this method is hashed into a character + number string, but now I need to hash a string into 10 binary numbers, there is no algorithm can be implemented, or you can md5 the results of conversion to 10 binary

Reply content:

MD5 this method is hashed into a character + number string, but now I need to hash a string into 10 binary numbers, there is no algorithm can be implemented, or you can md5 the results of conversion to 10 binary

In general, the conversion of a string to a decimal number can be done with the CRC32 () function ...

The advantage of this function is that it is fast ... Within my knowledge this should be the fastest built-in hash function in PHP ...

The disadvantage is that the operating system relies on ... Functions of different operating systems crc32() will produce a value that is not the same ...

As md5() for turning the results into decimal ... With Hexdec () is obviously not reliable ... Reasons like upstairs said not to repeat ...

Here we use another more flexible conversion function base_convert () ...

The benefit of this function is that it returns a string ... There is no overflow problem ...

The sample code is as follows ...

 

Hash into ten , as big number p>

You can use the PHP hexdec function, the results of MD5, because Hexdec can only turn 0x7fffffff, and then big becomes float.

In addition, you do not have to use MD5, because the result of MD5 is 128bit, the PHP language itself cannot directly save such a large number of decimal. You can refer to Sdbmhash, Jshash, or C + + STL for the hash of the string itself, such as this is the Python version of the Sdbmhash

def Sdbm_hash_string (str): h = 0 m = (1 <<) for i in str:t = h h = (t << 6)% m + (t &L t;<)% m-t + ord (i) H%= m return h 

Finally, by the way, do not attempt to split the MD5 results into a few paragraphs and then XOR and the like, the effect is not directly take the last few bytes.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.