Hash Function

A hash function is an algorithm that turns data of any size into a fixed-length string, called a hash, that acts like a digital fingerprint. The same input always gives the same hash, and the process cannot practically be reversed.

A hash function takes any input, such as a word, a file or a block of transactions, and runs it through a fixed mathematical procedure to produce a short output of set length, called a hash or digest. Cryptographic hash functions add security properties. Changing even one character of the input produces a completely different output. It is computationally infeasible to work backwards from a hash to its input, or to find two different inputs that share the same hash.

Blockchains depend on these properties. Each block contains the hash of the previous block, so altering past data would change every later hash and be detected at once. In proof of work, miners repeatedly hash block data with different nonces until they find a result below a target. Transaction IDs, Merkle trees and many address formats are also built from hashes.

For example, SHA-256, used by Bitcoin, always outputs 256 bits, usually shown as 64 hexadecimal characters, whether the input is a single letter or an entire file. Hashing "hello" and "Hello" gives two outputs that look entirely unrelated.

A common misconception is that hashing is encryption. Encryption is designed to be reversed with a key, while hashing is one-way and uses no key. Hashing does not hide short or predictable inputs, which can be guessed by hashing likely candidates and comparing the results. Older functions such as MD5 and SHA-1 are no longer considered collision-resistant.

Learn more

Related terms