Update
This commit is contained in:
parent
f82f156102
commit
8c0c5465f1
1 changed files with 38 additions and 5 deletions
43
hash.md
43
hash.md
|
@ -27,17 +27,30 @@ 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.
|
||||
|
||||
## Code Example
|
||||
## 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 the resulting hash.
|
||||
Let's say we want a hash 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 the resulting hash.
|
||||
|
||||
First thought 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 (so similar strings such as "A" and "B" will give similar hashes, which we don't want). 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.
|
||||
|
||||
TODO
|
||||
A better hash function for strings is shown in the section below.
|
||||
|
||||
## Nice Hashes
|
||||
|
||||
{ Reminder: I make sure everything on this Wiki is pretty copy-paste safe, I only copy short (probably uncopyrightable) snippets or public domain code and additionally also reformat and change them a bit, so don't be afraid of the snippets. ~drummyfish }
|
||||
{ Reminder: I make sure everything on this Wiki is pretty copy-paste safe, from the code I find on the Internet I only copy extremely short (probably uncopyrightable) snippets of public domain (or at least free) code and additionally also reformat and change them a bit, so don't be afraid of the snippets. ~drummyfish }
|
||||
|
||||
Here is a simple and pretty nice 8bit hash, it outputs all possible values and all its bits look quite random: { Made by me. ~drummyfish }
|
||||
|
||||
```
|
||||
uint8_t hash(uint8_t n)
|
||||
{
|
||||
n *= 23;
|
||||
n = ((n >> 4) | (n << 4)) * 11;
|
||||
n = ((n >> 1) | (n << 7)) * 9;
|
||||
|
||||
return n;
|
||||
}
|
||||
```
|
||||
|
||||
The [*hash prospector* project](https://github.com/skeeto/hash-prospector) ([unlicense](unlicense.md)) created a way for automatic generation of integer hash functions with nice statistical properties which work by [XORing](xor.md) the input value with a bit-shift of itself, then multiplying it by a constant and repeating this a few times. The functions are of the format:
|
||||
|
||||
|
@ -73,4 +86,24 @@ uint16_t hash(uint16_t n)
|
|||
}
|
||||
```
|
||||
|
||||
TODO: more (strings etc.)
|
||||
Here is a nice string hash, works even for short strings, all bits look pretty random: { Made by me. ~drummyfish }
|
||||
|
||||
```
|
||||
uint32_t strHash(const char *s)
|
||||
{
|
||||
uint32_t r = 21;
|
||||
|
||||
while (*s)
|
||||
{
|
||||
r = (r * 31) + *s;
|
||||
s++;
|
||||
}
|
||||
|
||||
r = r * 4451;
|
||||
r = ((r << 19) | (r >> 13)) * 5059;
|
||||
|
||||
return r;
|
||||
}
|
||||
```
|
||||
|
||||
TODO: more
|
Loading…
Reference in a new issue