Hash table, which is based on the high-speed access point of view design, but also a typical " space-time " approach. As the name implies, the data structure can be understood as a linear table, but the elements are not tightly arranged, but there may be gaps.

A hash table, also known as a hash table, is a data structure that is directly interviewed based on key value. That is, it visits records by mapping key code values to a location in the table to speed up the search. This mapping function is called a hash function, and the array that holds the record is called the hash table.

For example, we store 70 elements, but we may have requested 100 elements of space for these 70 elements. 70/100=0.7, this number is called the load factor. The reason we do this is also for the purpose of "High speed access". We arrange a storage location for each element based on a result of a random, evenly distributed fixed function h, which avoids traversing linear searches to achieve high-speed access. But because of this randomness, it must also lead to a problem is conflict. The so-called conflict, that is, two elements through the hash function h to get the same address, then these two elements are called "synonyms." This is similar to 70 people going to a restaurant with 100 chairs for dinner. The hash function evaluates to a storage unit address, and each storage unit is called a bucket. To set a hash list with M buckets, the value of the hash function should be [0,m-1].Conflict resolution is a complex issue. The conflict depends mainly on:(1) hash function, the value of a good hash function should be distributed as evenly as possible.(2) dealing with conflicting methods.(3) The size of the load factor. Too big is not necessarily good, and wasted space is serious, load factor and hash function is linkage.Resolution of the conflict:(1) Linear probing method: After the conflict, linear forward heuristic, find a recent empty position. The disadvantage is that there will be a stacking phenomenon. When accessed, words that may not be synonyms are also located in the probing sequence, affecting efficiency.(2) Double hash function method: After the conflict of position D, again using a hash function to produce a hash list bucket capacity m coprime number C, then heuristic (d+n*c)%m, so that the exploration sequence jump-type distribution.Frequently used methods for constructing hash functions

The hash function makes the access to a data series more efficient, and the data elements are positioned more quickly through the hash function:

1. Direct addressing method: take keyword or keyword a linear function value is a hash address. That is, H (key) =key or H (key) = A?key + B, where A and B are constants (such hash functions are called self functions)

2. Digital analysis: analysis of a set of data, such as the date of birth of a group of employees, when we found that the first few numbers of the date of birth is roughly the same, the likelihood of conflict will be very large, but we found that the number of months and days after the month and the detailed date of the difference is very large, Assuming that the following numbers are used to form a hash address, the odds of a conflict are significantly reduced. Therefore, the digital analysis method is to find out the laws of numbers, as far as possible to use this data to construct a low probability of conflict hash address.

3. The square takes the middle method: takes keyword square after the intermediate several as the hash address.

4. Folding method: The keyword is cut into the same number of bits, the last part of the number can be different, and then take these parts of the overlay and (remove carry) as the hash address.

5. Random number method: Select a random function, take keyword random value as the hash address, pass often used in keyword length different occasions.

6. In addition to the remainder of the method: Take keyword is not greater than the hash table length m of the number of p after the remainder is a hash address. That is, H (key) = key MOD p, p<=m. Not only can the keyword directly take the mold, but also in the folding, square take the medium operation after the modulo. The choice of P is very important, generally take prime or m, if p is not good, easy to produce synonyms.Performance analysis for lookups

The hash table lookup process is essentially the same as the watchmaking process. Some key codes can be found directly through the address of the hash function transformation, and there are some key codes in the hash function to get the address of the conflict, you need to deal with the method of conflict to find. In the three methods of dealing with conflicts, post-conflict lookups are still the process of comparing a given value to a key code. Therefore, the measurement of the efficiency of the hash table is still measured by the average lookup length.

In the process of searching, the number of key codes depends on how many conflicts are generated, the conflict is less, the search efficiency is high, the conflict is more, and the search efficiency is low. Therefore, the factors that affect the number of conflicts, that is, the factors that affect the search efficiency. There are three factors that affect the number of conflicts:

1. The hash function is uniform;

2. Methods of dealing with conflicts;

3. Reload factor for the hash table.

The reload factor for the hash list is defined as: α= the number of elements in the table/length of the hash list

α is the marker factor for the full extent of the hash table. Because the length of the table is fixed, α is proportional to the number of elements in the table, so the larger the alpha, the more elements are filled in the table, the greater the likelihood of a conflict, and the smaller the alpha, the less likely it is to have a conflict.

In fact, the average lookup length of a hash table is a function of filling factor α, and only different methods of dealing with conflicts have different functions.

Understanding the basic definition of hash, you can not mention some famous hash algorithm, MD5 and SHA-1 can be said to be the most widely used hash algorithm, and they are based on MD4 design. So what do they mean?

Here's a quick look:

(1) MD4

MD4 (RFC 1320) was designed by MIT's Ronald L. Rivest in 1990, MD is the abbreviation for Message Digest. It is implemented on a 32-bit word processor with a fast software implementation-it is based on a bitwise operation of 32-bit operands.

(2) MD5

MD5 (RFC 1321) is an improved version number of Rivest for MD4 in 1991. It still groups the input with 512 bits, and its output is a cascade of 4 32-bit words, same as MD4. MD5 is more complex than MD4 and slower, but safer, better in terms of resistance to analysis and differential resistance

(3) SHA-1 and others

The SHA1 is designed by the NIST NSA to be used with the DSA, which produces a hash value of 160bit in length for inputs of less than 264, thus providing better anti-brute-force. The SHA-1 design is based on the same principles as the MD4, and mimics the algorithm.

A hash table inevitably conflicts (collision) Phenomenon: keyword may get the same hash address as Key1≠key2, and hash (key1) =hash (Key2) for different types. Therefore, when building a hash table, you should not only set a good hash function, but also set a method for dealing with conflicts. A hash table can be described as follows: According to the set hash function H (key) and the selected method of dealing with conflicts , a set of keyword images to a finite , sequential address Address set (interval) and keyword the "elephant" in the address set as the storage location for the corresponding record in the table, such a table is called a Hashtable.

For a dynamic lookup table, 1) The table length is indeterminate; 2) When designing a lookup table, you only know the scope of the keyword, and you do not know the exact keyword. Therefore, the normal situation needs to establish a function relationship, with F (key) as the keyword key is recorded in the table position, usually called this function f (key) is a hash function. (Note: This function is not necessarily a mathematical function)

A hash function is an image that maps a collection of keyword to an address set, and its settings are very flexible, just so that the size of the address collection does not exceed the scope of consent.

In reality, the function of the hash is to be constructed, and the good ability of construction is good to use.

So what's the use of these hash algorithms?

The application of hash algorithm in information security main body now the following 3 aspects:

(1) file check

We are more familiar with the parity check algorithm and CRC check, these 2 kinds of verification does not have the ability to resist data tampering, they can detect and correct the transmission of the channel error in the data, but it does not prevent malicious damage to the data.

The "digital fingerprint" feature of MD5 hash algorithm makes it become the most widely used file integrity checksum (Checksum) algorithm, and many UNIX systems have the command to provide calculation MD5 Checksum.

(2) digital signature

Hash algorithm is also an important part of modern password system. Because of the slow operation of the asymmetric algorithm, the one-way hash function plays an important role in the digital signature protocol. The Hash value, also known as "Digital Digest", is digitally signed and can be statistically equivalent to a digital signature on the file itself. And there are other advantages to this agreement.

(3) authentication Agreement

For example, the following authentication protocol is also known as the Challenge-authentication mode: This is a simple and secure way to be able to listen to a transmission channel but not tamper with it.

File hash value

The Digital Digest of the md5-hash-file is computed by the Hash function. Regardless of the length of the file, its hash function evaluates to a fixed-length number. Unlike cryptographic algorithms, this hash algorithm is an irreversible one-way function. With a high-security hash algorithm, such as MD5, Sha, two different files almost impossible to get the same hash results. Therefore, once the file has been altered, it can be detected.

The hash function also has a different meaning. The actual hash function refers to the mapping of a large range to a small range. The goal of mapping a wide range to a small area is often to save space and make data easy to save. In addition, hash functions are often applied to lookups. Therefore, before considering the use of the hash function, it is necessary to specify several limitations:

1. The main principle of hash is to map a large range to a small range, so the number of actual values you enter must be equal to or smaller than the small range. Otherwise there will be very much conflict.2. Because the hash approximates a one-way function, you can use it to encrypt the data.3. Different applications have different requirements for the hash function, for example, the hash function for encryption mainly considers the gap between it and the single function, while the hash function used for searching mainly considers its mapping to a small range of conflict rates.The hash function applied to encryption has been discussed too much, in the author's blog has more specific introduction. Therefore, this article only explores the hash function used for lookups.The main object used by the hash function is an array (for example, a string), and its target is usually an int type. We all follow this way.In general, the hash function can be easily divided into such as the following categories:1. Addition Hash;2. bit operation Hash;3. Multiplication hash;4. Division Hash;5. Check the table hash;6. Mixed hash;The following specific introduction of the above-mentioned various methods in the practical application.An addition hashThe so-called additive hash is the addition of the input elements of a single sum to form the final result. The construction of the standard addition hash is for example the following:

Here the prime is random prime number, see, the result of the range is [0,prime-1].

Two-bit arithmetic hashThis type of hash function fully mixes the input elements by taking advantage of the various bitwise operations (common is shift and XOR). For example, the construction of a standard rotational hash is as follows:

The main feature of this type of hash function is to shift first and then perform various bit operations. For example, the above-calculated hash code can also have several variants such as the following:

Three-multiplication HashThis type of hash function takes advantage of the non-correlation of multiplication (the nature of multiplication, the most famous of which is the random number generation algorithm of the square Kinsoku, although the algorithm does not work well). Example

The Hashcode () method of the string class inside the jdk5.0 also uses the multiplication hash. Just, it uses a multiplier of 31. The recommended multipliers are: 131, 1313, 13131, 131313, and so on.The well-known hash functions in this way are:

Although the application of the ADLER32 algorithm is not CRC32 widely, it is probably the most famous one in the multiplication hash. On its introduction, you can see the RFC 1950 specification.

Four-Division HashLike division and multiplication, the same has a seemingly non-correlation on the surface. Just because the division is too slow, this way is almost no real application. It is important to note that the result of the hash we see in the previous divided by a prime is simply to guarantee the scope of the result. Assuming you don't need it to limit a range, you can replace "Hash%prime" with code such as the following: hash = hash ^ (hash>>10) ^ (hash>>20).Five look-up table hashThe most famous example of tabular hash is the CRC series algorithm. Although the CRC series algorithm itself is not a check table, but the table is one of its fastest way to implement. The following is the implementation of CRC32:

The famous examples in tabular hash are: Universal Hashing and Zobrist Hashing. Their tables are randomly generated.

Six mixed hashThe hybrid hash algorithm takes advantage of these various methods. A variety of common hash algorithms, such as MD5, Tiger, belong to this range. They are generally very rarely used in lookup-oriented hash functions.

Evaluation of seven-pair hash algorithmHttp://www.burtleburtle.net/bob/hash/doobs.html This page provides an evaluation of several popular hash algorithms. Our suggestions for hash functions are as follows:

1. Hash of the string. The simplest can use the main multiplication hash, when the multiplier is 33 o'clock, the English word has a very good hash effect (less than 6 lowercase form can guarantee no conflict). A bit more complex is the ability to use the FNV algorithm (and its improved form), which is good at both speed and effect for longer strings.

2. Hash of the long array. The ability to use an algorithm like HTTP://BURTLEBURTLE.NET/BOB/C/LOOKUP3.C, which calculates multiple bytes at once, is a good speed.

The principle of common hash algorithm

This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to info-contact@alibabacloud.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.

Try 40+ Products For Free !

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.