2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Hash, also known as hash, is the basis of cryptography. Understanding hash is a necessary prerequisite for understanding technologies such as digital signatures and encrypted communications.
In C++, hashing is often used to encrypt strings so that different strings have different values.
At the same time, hashing is irreversible, so it is often used to encrypt passwords.
The principle of hashing is actually very simple, which is to regard the string as a polynomial number, and then convert the number into a decimal number, which is the result of the hashing.
Knowing the principle of hashing, it is very easy to write code.
const int base=131;//这里是将字符串看成的进制
int hash(string str) {
int ans=0;
for(int i=0;i<str.size();++i) ans=ans*base+(int)str[i];
return ans;
}
There are many other hashing methods, such as double hashing, but only one hash is enough.