Hash hash (1)

Source: Internet
Author: User
Hash hash (1)

Hash is a common term and is often used in programming. However, most people know it, but they do not know it. In addition, they want to write a consistent hash algorithm over the past few days, suddenly I think it is not very clear about hash, so I will take some time to summarize the hash knowledge. I have read a lot of blog posts and thank you for your selfless sharing.

Basic Concepts

Hash, which is usually translated as "hash" and is also directly translated as "hash. So what is the hash function? This is probably the value = hash (key). We want the key and value to be uniquely mapped.

Hash Tables (also called hash tables) are the most commonly used data structures that are directly accessed based on key values, by ing the key value to a position in the table, you can access the record to speed up the search. This ing function is called a hash function or a hash function.

Hash has two main applications: encryption and compression. In terms of encryption, Hash hash converts information of different lengths into messy 128-bit codes. These encoding values are called hash values, the most widely used Hash algorithms are md4, MD5, and sha1. In terms of compression, hash refers to ing a large range to a small range, which is often used to save space and make data easy to save.

Hash features

The main principle is to map a large range to a small range. Therefore, the input range must be equal to or smaller than a small range. Otherwise, conflicts will increase.

Hash Functions approach unidirectional functions, so they can be used to encrypt data. (Single function: If a function is given input, it is easy to calculate the result. However, it is difficult to calculate the input when the result is given)

Different applications have different requirements for the hash function: the hash function used for encryption mainly considers the gap between it and a single function, the hash function used for searching mainly considers the conflict rate mapped to a small range.

Hash Algorithm

Hash generation methods can be divided into three basic methods: addition, multiplication, and shift.

The addition hash is to traverse the elements in the data and then add an initial value each time. The values in the hash are related to an element of the data.

/********************************** Add hash * @ key input string * @ prime number ******************************/INT additivehash (string key, int prime) {int hash, I; for (hash = key. length (), I = 0; I <key. length (); ++ I) {hash + = int (key. at (I);} return (hash % prime );}

Multiplication hash is used to traverse the elements in the data and perform multiplication on the initial values each time. The multiplication value does not need to be related to the data.

/********************************** Multiplication hash * @ key input string * @ prime number ******************************/INT Bernstein (string key) {int hash, I; for (hash = 0, I = 0; I <key. length (); ++ I) {hash = 33 * hash + int (key. at (I);} return hash ;} /*********************************** 32-bit FNV algorithm (Multiplication) * @ key input string * @ prime number ******************************* */INT m_shift = 0; int m_mask = 0x8765fed1; int fnvhash (string key) {int hash = (INT) 2166136261l; For (INT I = 0; I <key. length (); ++ I) {hash = (hash * 16777619) ^ int (key. at (I);} If (m_shift = 0) return hash; Return (hash ^ (hash> m_shift) & m_mask ;}

In Java, the hash function uses a multiplication hash:

/*** Java Algorithm */public static int Java (string Str) {int h = 0; int off = 0; int Len = Str. length (); For (INT I = 0; I <Len; I ++) {H = 31 * H + Str. charat (Off ++);} return h ;}

The shift hash operation is to traverse the elements in the data and then shift the initial values each time.

/********************************** Rotating Hash (shift) * @ key input string * @ prime number ******************************* */INT rotatinghash (string key, int prime) {int hash, I; for (hash = key. length (), I = 0; I <key. length (); ++ I) {hash = (hash <4) ^ (hash> 28) ^ int (key. at (I);} return (hash % prime );}

In practice, many hash functions include addition, multiplication, and shift operations, such as MD5.

Problem

Why is the prime value a prime number? Is there a scientific basis? To be verified

Some people say that the prime number can reduce the collision probability. However, the reason is not well explained. If any one has an idea, I hope to share it with you.

Hash hash (1)

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.