In mathematics, a function is a process whereby one or more inputs are transformed into an output. A hash function takes an input of indeterminate length and mathematically transforms it into an output of fixed, pre-determined, usually shorter length. The function "hashes" the input. The output of a hash function, commonly referred to as the hash or the digest, is determined by the hashing such that the same input always produces the same hash. The output of a hash function is usually shorter than the input, and one of the benefits of hashing data is often data compression.
The following characteristics make certain hash functions desirable for cryptographic applications:
Other, less cryptographically friendly attributes of hash functions might include compression - taking very large inputs or input strings to be indexed into shorter, fixed-length digests to be used as an index - and familiarity so that similar inputs have similar but different outputs so that hashing is faster, though not safer.
Cryptographic Hash Functions
While there are numerous hash functions in common use, the ones used most frequently in encryption are MD5 (Message Digest 5) and SHA256 (Secure Hash Algorithm 256). Satoshi Nakamoto's "bitcoin whitepaper" refers to SHA-256 as usable for the proof-of-work in a bitcoin blockchain.
As mentioned, hash functions produce output of a fixed length. Here are two examples of the use of MD5 hashing:
These examples illustrate a few important characteristics of a hash function. For one, the input strings are of different lengths, one is for a quotation and the other is a number. Secondly, the digests are different but of identical 32-bit length. Thirdly, the digest uses hexadecimal digits (which are 0 to 9 and a to f). While common, not all cryptographic functions use 32-bit, hexadecimal output and bitcoin transaction hash digests are 64 characters. SHA256 is used for the bitcoin blockshain. Here is how the same inputs as above are hashed using SHA256:
Again, the inputs are of different lengths but both result in 64 bit, hexadecimal digests.
Hash Functions in Blockchains
Blockchains are series of blocks of transactions. For purposes of data compression as well as privacy and security, transaction information is hashed before it is included in a block. Each block includes a hash of the last block which leads to the creation of the chain. Because cryptographic hash functions are deterministic and highly collision resistant, including a hash of the last previous block effectively prevents anyone tampering with its contents once the next block has been formed.