Update hash

This commit is contained in:
Miloslav Ciz 2022-03-17 20:01:26 +01:00
parent 49a1542551
commit a7cd59daf0

18
hash.md
View file

@ -2,13 +2,25 @@
Hash is a number computed by a **hash function**, a function that takes some data and turns it into a number (the hash) that's much smaller than the data itself, has a fixed size (number of [bits](bit.md)) and which has additional properties such as being completely different from hash values computed from very similar data. Thanks to these properties hashes have a very wide use in [computer science](compsci.md) -- they are often used to quickly compare whether two pieces of non-small data, such as documents, are the same, they are used in indexing structures such as **hash tables** which allow for quick search of data, and they find a great use in [cryptocurrencies](crypto.md) and [security](security.md), e.g. for [digital signatures](sigital_signature.md). Hashing is extremely important and as a programmer you won't be able to avoid encountering hashes somewhere in the wild. Hash is a number computed by a **hash function**, a function that takes some data and turns it into a number (the hash) that's much smaller than the data itself, has a fixed size (number of [bits](bit.md)) and which has additional properties such as being completely different from hash values computed from very similar data. Thanks to these properties hashes have a very wide use in [computer science](compsci.md) -- they are often used to quickly compare whether two pieces of non-small data, such as documents, are the same, they are used in indexing structures such as **hash tables** which allow for quick search of data, and they find a great use in [cryptocurrencies](crypto.md) and [security](security.md), e.g. for [digital signatures](sigital_signature.md). Hashing is extremely important and as a programmer you won't be able to avoid encountering hashes somewhere in the wild.
It is good to know that we distinguish between "normal" hashes used for things such as indexing data and [cryptographic](cryptography.md) hashes that are used in computer security and have to satisfy some stricter mathematical criteria. For the sake of simplicity we will sometimes ignore this distinction here. Just know it exists.
It is generally given that a hash (or hash function) should satisfy the following criteria: It is generally given that a hash (or hash function) should satisfy the following criteria:
- **Have fixed size** (given in bits), even for data that's potentially of variable size (e.g. text strings). - **Have fixed size** (given in bits), even for data that's potentially of variable size (e.g. text strings).
- **Be fast to compute**. This is mostly important for non-security uses, cryptographic hashes may prioritize other properties to guarantee the hash safety. - **Be fast to compute**. This is mostly important for non-security uses, cryptographic hashes may prioritize other properties to guarantee the hash safety.
- **Have uniform mapping**. That is if we hash a lot of different data the hashes we get should be uniformly spread over the space of the hashes, i.e. NOT be centered around some number. This is in order for hash tables to be balanced, and it's also required in security (non-uniform hashes can be easier to reverse). - **Have uniform mapping**. That is if we hash a lot of different data the hashes we get should be uniformly spread over the space of the hashes, i.e. NOT be centered around some number. This is in order for hash tables to be balanced, and it's also required in security (non-uniform hashes can be easier to reverse).
- **Behave in a [chaotic](chaos.md) manner**, i.e. hashes of similar data should be completely different. This is similar to the point above; a hash should kind of appear as a "random" number associated to the data (but of course, the hash of the same data has to always be the same when computed repeatedly, i.e. be [deterministic](determinism.md)). So if you change just one bit in the hashed data, you should get a completely different hash from it. - **Behave in a [chaotic](chaos.md) manner**, i.e. hashes of similar data should be completely different. This is similar to the point above; a hash should kind of appear as a "random" number associated to the data (but of course, the hash of the same data has to always be the same when computed repeatedly, i.e. be [deterministic](determinism.md)). So if you change just one bit in the hashed data, you should get a completely different hash from it.
- **Minimize collision**, i.e. the probability of two different values giving the same hash. Mathematically collisions are always possible if we're mapping a big space onto a smaller one, but we should try to reduce collisions that happen in practice. This property should follow from the principle of uniformity and chaotic behavior mentioned above. - **Minimize collisions**, i.e. the probability of two different values giving the same hash. Mathematically collisions are always possible if we're mapping a big space onto a smaller one, but we should try to reduce collisions that happen in practice. This property should follow from the principle of uniformity and chaotic behavior mentioned above.
- **Be difficult to reverse** (mainly for security related hashes). Lots of times this comes naturally from the fact that a hash maps a big space onto a smaller space (i.e. it is a non-[injective](injective.md) function). Hashes can typically be reversed only by [brute force](brute_force.md). - **Be difficult to reverse** (mainly for security related hashes). Lots of times this comes naturally from the fact that a hash maps a big space onto a smaller space (i.e. it is a non-[injective](injective.md) function) and from their chaotic nature. Hashes can typically be reversed only by [brute force](brute_force.md).
TODO: example, hash tables, uses Hashes are similar to [checksums](checksum.md) but are different: checksums are simpler because their only purpose is for checking data integrity, they don't have to have a chaotic behavior, uniform mapping and they are often easy to reverse. Hashes are also different from database IDs: IDs are just sequentially assigned numbers that aren't derived from the data itself, they don't satisfy the hash properties and they have to be absolutely unique.
Some common uses of hashes are:
- [Hash tables](hash_table.md), [data structures](data_structure.md) that allows for quick search and access of data. For example in [chess](chess.md) programs and databases hashes of chess positions are used to identify and get some information associated with the position.
- [Passwords](password.md) in user databases are for security reasons not stored as plain text, instead only password hashes are stored. When a user enters a password, the system computes its hash and compares it to that stored in the database: if the hashes match, the password was correct. This is a way of allowing password authentication without giving the system the knowledge of user passwords.
- In [digital signatures](digital_signature.md) hashes of documents are used to prove a document hasn't been modified by a third party.
- [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