War stories
A long time ago, there were many war stories about hash functions. The basic principle of those wars is to carefully construct a large number of hash conflicts to occupy a large amount of CPU resources.
The following is an example of the software under attack:
-
Servers with vulnerabilities: attackers carefully construct ha conflicts, and only need a K network speed to crash the server to achieve DOS attacks.
-
Perl 5.8.0: attackers carefully construct ha conflicts and insert them to the associated array.
-
Linux 2.4.20 kernel: attackers carefully construct file names, resulting in a large number of HA conflicts, resulting in a sudden drop in system performance.
Attack principles
In Java, the String object is easy to construct ha system conflicts. Demonstrate examples of HA conflicts in Java.
Solution
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Csq508O4/LzTuN + signature/Signature + u0wdXiwO + signature + cve-vcd4kpha + signature + qz/Signature + signature/Signature + CjxwPgo8YnI + cda-vcd4kpha + CrbAwaLBtLHto7o8L3A + Cjx1bCBjbGFzcz0 = "list-paddingleft-2">
-
Easy to delete Elements
-
As the data volume increases, performance decreases slowly
-
Ha conflicts have little impact on the system.
Linear probes:
-
Use less memory because there is no linked list
-
Better cache Performance
Improvement of Hash Functions
At present, many different hash algorithms have been implemented.
Dual-value hash:
A hash function returns two hash values, which are inserted into a short chain when an element is inserted. This method can reduce the expected length of the chain.
Double hash:
The linear probe method is used, but different numbers of elements are skipped after each conflict to find the vacancy. This method can eliminate consecutive placeholder values, so that the hash table can be almost filled up, But deletion is difficult to implement.
Cuckoo hash:
First generate a hash and calculate a location. If there is a conflict, add some parameters to continue the hash and calculate another location. Until the vacant position is found. In the worst case, the complexity of this method is N.
Comparison between HA table and binary tree
Both the hash table and the balance tree can be associated with arrays.
Hash table:
-
Simple code
-
This method has the best performance
-
This method is faster for simple keywords.
-
It has better system support in java. For example, the hashCode cache is used in String.
Binary Tree:
-
Better Performance Assurance
-
Supports sequential operations (obtain the ranking of an element, the nth value, and the number of elements X to Y)
-
CompareTo functions are easier to implement than hashCode functions and are not prone to errors.
Both methods are implemented in the Java library. Java. util. TreeMap java. util. TreeSet is implemented through the red/black tree, and java. util. HashMap java. util. IdentityHashMap is implemented through the hash table.