Update
This commit is contained in:
parent
a2c3e6e920
commit
ae3d45b6b4
1 changed files with 22 additions and 1 deletions
23
hash.md
23
hash.md
|
@ -23,4 +23,25 @@ Some common uses of hashes are:
|
|||
- [Digital fingerprints](fingerprint.md) are hashes computed from known data about a user. The fingerprint is a small number that identifies a tracked user.
|
||||
- In [blockchain](blockchain.md) based on proof of work the computational difficulty of reversing a hash is used in the process of mining as a puzzle whose solution is rewarded. Miners compete in finding bits such that if appended to a newly added block will result in the block's hash being some defined number.
|
||||
|
||||
TODO: example
|
||||
## Code Example
|
||||
|
||||
Let's say we want a has function for string which for any [ASCII](ascii.md) string will output a 32 bit hash. How to do this? We need to make sure that every character of the string will affect change the resulting hash.
|
||||
|
||||
First though that may come to mind could be for example to multiply the ASCII values of all the characters in the string. However there are at least two mistakes in this: firstly short strings will result in small values as we'll get a product of fewer numbers. Secondly reordering the characters in a string (i.e. its [permutations](permutation.md)) will not change the hash at all (as with multiplication order is insignificant)! These violate the properties we want in a hash function. If we used this function to implement a hash table and then tried to store strings such as "abc", "bca" and "cab", all would map to the same hash and cause collisions that would negate the benefits of a hash table.
|
||||
|
||||
The following is a better function, a variant of a function used in sdbm:
|
||||
|
||||
```
|
||||
uint32_t stringHash(const char *s)
|
||||
{
|
||||
uint32_t result = 123;
|
||||
|
||||
while (*s)
|
||||
{
|
||||
r = s + (result << 16) + (result << 6) - result;
|
||||
s++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
Loading…
Reference in a new issue