From ae3d45b6b494dafbec1cf74ed65b6ce5a2c24f03 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Fri, 18 Mar 2022 22:20:11 +0100 Subject: [PATCH] Update --- hash.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/hash.md b/hash.md index f87c61f..fe91e03 100644 --- a/hash.md +++ b/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 \ No newline at end of file +## 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; +} +``` \ No newline at end of file