What Is a Hash?
A hash is a mathematical function that converts an input of arbitrary length into an encrypted output of a fixed length. Thus, regardless of the original amount of data or file size involved, its unique hash will always be the same size. Moreover, hashes cannot be used to "reverse-engineer" the input from the hashed output since hash functions are "one-way" (like a meat grinder; you can't put the ground beef back into a steak). Still, if you use such a function on the same data, its hash will be identical, so you can validate that the data is the same (i.e., unaltered) if you already know its hash.
Hashing is also essential to blockchain management in cryptocurrency.
- A hash is a function that meets the encrypted demands needed to secure information.
- Hashes are of a fixed length, making it nearly impossible to guess the hash if someone was trying to crack a blockchain.
- The same data will always produce the same hashed value.
- Hashes are one of the backbones of the blockchain network.
- A hash is developed based on the information present in a block header.
How Hashes Work
Typical hash functions take inputs of variable lengths to return outputs of a fixed length. A cryptographic hash function combines the message-passing capabilities of hash functions with security properties. Hash functions are algorithms that determine how information is encrypted.
For example, Secure Hashing Algorithm 256 (SHA-256) goes through a process to encrypt the input it receives by:
- Converting it to binary
- Creating hash values
- Initializing constants
- Chunking data into bits
- Creating a message schedule
- Running a compression loop
- Modifying the final values
Using SHA-256, the word “Hello” will produce an output that is the same number of characters (64) as "Hello world" and “Hello John." However, the hash will be significantly different for all three—keep in mind that capital letters change the hash also:
- Hello: 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
- Hello world: 64ec88ca00b268e5ba1a35678a1b5316d212f4f366b2477232534a8aeca37f3c
- Hello John: a8119595d77342cc73c93697a7f70920d3f4ded5d458e31907607e997ff76868
The function used to generate the hash is deterministic, meaning it will produce the same result each time the same input is used. SHA 256 can generate a hashed output in milliseconds with very little computing power, but it also makes determining the input difficult. This makes hashing ideal for securing cryptocurrency because it would take thousands of years to reverse the encryption to determine the original input with modern technology.
Hash functions are commonly used data structures in computing systems for tasks such as checking the integrity of messages and authenticating information. Cryptographic hash functions add security features, making detecting the contents of a message or information more difficult.
In particular, cryptographic hash functions exhibit these three properties:
- They are collision-free: This means that no two different input hashes should map to the same output hash.
- They can be hidden: It is difficult to guess the input value for a hash function from its output.
- They should be puzzle-friendly: It should be difficult to select an input that provides a predefined output. Thus, the input should be selected from a distribution that's as wide as possible.
Because of the features of a hash, they are used extensively in online security—from protecting passwords to detecting data breaches to checking the integrity of a downloaded file.
Hashing and Cryptocurrencies
The backbone of a cryptocurrency is the blockchain, which is a globally distributed ledger formed by linking together individual blocks of transaction data through hashing. The blockchain only contains validated transactions, which prevents fraudulent transactions and double spending of the currency. Cryptocurrency mining and validation involves working with this hash.
Solving a cryptocurrency hash starts by using the block header from the previous block as input and generating a hash. Each block header contains a version number, a timestamp, the hash used in the previous block, the hash of the Merkle root, the nonce, and the target hash.
The goal is to generate a hash that is equal to or less than the network's target hash. In the hash is a sequence of numbers called the nonce, or number used once. The mining program focuses on the nonce, which starts at zero in the first attempt. If the attempt fails, the program adds 1 to the nonce, and generates the hash again. It adds 1 to each failed attempt until generating a hash less than or equal to the target hash, then it is accepted as the solution.
Solving the hash requires the miner to continue trying to generate the right value, which requires a significant amount of trial-and-error. The miner who solves the hash is given the reward, and the block is added to the blockchain.
It is highly unlikely that a miner will successfully come up with the correct nonce on the first try, meaning that the miner may potentially test a large number of nonce options before getting it right. The greater the difficulty—a measure of how hard it is to create a hash that meets the requirement of the target hash—the longer it is likely to take to generate a solution.
Hash functions are algorithms that transform or "map" a given set of data into a bit string of fixed size, also known as the "hash."
A hash function utilizes complex algorithms that convert data of arbitrary length to data of fixed length (for instance, 256 characters). If you change one bit anywhere in the original data, the entire hash value changes, making it useful for verifying the fidelity of digital files and other data.
Hashes are used in several parts of a blockchain system. Each block header contains the previous block's hash, which ensures that nothing has been tampered with as new blocks are added. Cryptocurrency blockchains use hashes to secure information and make the ledger immutable.
The Bottom Line
In a cryptocurrency blockchain, a hash is a deterministic hexadecimal number. This means that no matter how many characters the input has, the hash will always be the same number of characters. For instance, Bitcoin's hashes are always 64 digits.
Hashes are used to secure information—in the case of cryptocurrency, they are used to ensure data contained in the blocks on a blockchain are not altered. The information encrypted by the hashing function is validated by network participants when they attempt to generate a hash less than the network target. Once the target hash is reached, the network closes the block—consensus is reached after the block closes because the network continues to validate transactions and block information after the hash is solved.
Investing in cryptocurrencies and other Initial Coin Offerings (“ICOs”) is highly risky and speculative, and this article is not a recommendation by Investopedia or the writer to invest in cryptocurrencies or other ICOs. Since each individual's situation is unique, a qualified professional should always be consulted before making any financial decisions. Investopedia makes no representations or warranties as to the accuracy or timeliness of the information contained herein. As of the date this article was written, the author does not own cryptocurrency.