Concepts and features hashing algorithm
We've shared hash table, hash functions and hash conflict, in fact, can also be translated as a hash table, hash function and hash collision, is a meaning. Hashing algorithm is simply understood that the aforementioned hash function for mapping a string of arbitrary length binary value to a binary value of a fixed length string, obtained after mapping the binary value is the hash value (hash value) .
Our daily development of the most common application is the hashing algorithm to encrypt the data by function md5, md5 is a hash function, combined with md5 hash algorithm we can sum up the general characteristics:
- Can not reverse a hash value derived from the raw data (it is also called a one-way hash algorithm algorithm, irreversible);
- Very sensitive to the input data, even if the original data is modified only one bit, the resulting hash value are very different;
- The probability of a hash collision to be very small, for different raw data, the probability of the same hash value is very small;
- The efficiency of hashing algorithm to be as efficient as possible for longer texts, can quickly calculate the hash value
Applying a hash algorithm
1, Scene One: Security Encryption
Daily user password encryption typically use are md5, sha hash function, etc., because it is not reversible, and the results differ greatly after minor differences encryption, so security is better.
2, Scene 2: unique identification
URL field or picture field such requirements can not be repeated, this time can be done by treatment of the corresponding field values md5, unified data and 32-bit length construct better query from the database index angle, in addition, also the class of documents do md5 binary data processing, as a unique identifier, so that when judging duplicate files more quickly.
3, Scene Three: data validation
For example, from the Internet to download a lot of files (especially P2P site resources), it will include a MD5 value, integrity check for downloading data, prevent data tampering was hijacked in the middle.
4, Scene Five: hash function
As already mentioned, PHP in md5, sha1, hash and other functions are hash algorithm to calculate a hash value based on
5, Scene Five: Load Balancing
For requests on the same client, in particular logged-in user's request, it needs to session requests are routed to the same machine, in order to ensure data consistency, which can make use of hashing algorithm to achieve, by the end user ID number modulo the total number of machines (how many bits can take a given number of machines), the resulting value as the machine number.
6, Scene 6: Distributed Cache
Distributed caching and other machines or distributed database is not the same as inconsistent cache data stored in each machine, the machine whenever the cache expansion, the need to re-index the cache storing the machine (or part of the re-index), here applied to the also thought hash algorithm.