Curriculum
Course: What is Crypto
Login
Video lesson

Cryptographic Hashing Function

Power of Cryptographic Hashing: Backbone of Blockchain Security

Behind the Blockchain Curtain: Securing the Crypto World

Imagine you had a magical black box that, no matter what you put into it—be it your name, a sentence, or the entire contents of Wikipedia—it always gave you the same neat, fixed-size output. Sounds too good to be true, right? Well, in the world of cryptography, this magic is real, and it’s known as cryptographic hashing. This fascinating technology doesn’t just keep your passwords safe; it’s also the very foundation upon which cryptocurrencies like Bitcoin are built.

In this lesson, we dive deep into the complexities of cryptographic hashing, breaking down how this seemingly simple process safeguards not only digital transactions but also entire decentralized ecosystems. Whether you’re a crypto enthusiast or just curious about the technological forces shaping modern finance, this exploration of hashing functions is an essential stop on your journey. Let’s uncover the magic behind this black box and see how it impacts both traditional finance and the rapidly evolving world of blockchain technology.


SHA-256: The Secure Hashing Algorithm Behind Bitcoin

At its core, cryptographic hashing serves as a tool for converting data into a unique string of characters, regardless of the data’s size. The lesson breaks this process down step by step, using the SHA-256 hashing function, which is the backbone of Bitcoin’s security protocol. The key points covered include:

  • Deterministic Nature: The same input always generates the same hash, ensuring consistency.
  • Fixed Output Size: No matter how much data is inputted, the output remains a 64-character string.
  • Efficiency: Hash functions are quick to compute, making them ideal for handling large sets of data.
  • Irreversibility: It’s impossible to reverse a hash to retrieve the original data, ensuring security.
  • Unpredictability: Even the slightest change in input drastically alters the hash, making it extremely secure against manipulation.

The lesson culminates by connecting hashing to Bitcoin’s Proof of Work system, explaining how miners solve cryptographic puzzles to validate transactions. This insight sets the stage for understanding why hashing isn’t just a neat cryptographic trick but a crucial part of decentralized finance (DeFi) and blockchain integrity.


Critical Analysis

Strengths of the Argument

One of the lesson’s greatest strengths is its accessible explanation of cryptographic hashing. By comparing it to a magical black box, the lesson takes a highly technical subject and makes it approachable to beginners. This is crucial, especially for newcomers to the crypto space, as concepts like hashing can otherwise feel intimidating.

Another strong point is the emphasis on the five essential properties of hashing, particularly the deterministic nature and irreversibility. These properties are key not just for understanding how hashing works but also for grasping why cryptocurrencies, which rely heavily on these functions, are so secure. The lesson highlights the importance of consistency in producing the same output for identical inputs, ensuring that blockchain data remains tamper-proof. For instance, when a Bitcoin transaction is hashed, any attempt to alter it would produce an entirely different result, immediately flagging the tampering.

The explanation of Proof of Work (PoW) also deserves praise. By illustrating how miners use brute force to find a hash that meets specific criteria, the lesson demystifies a process that often seems opaque. This detailed explanation helps bridge the gap between cryptographic theory and real-world application, providing clarity on how Bitcoin mining secures the blockchain.

Potential Weaknesses and Limitations

Despite these strengths, the lesson could have explored more of the nuances behind hashing algorithms beyond SHA-256. While focusing on SHA-256 makes sense for a Bitcoin-centric discussion, it glosses over the variety of hashing functions in use today. For instance, Ethereum currently uses a different algorithm, Keccak-256, and newer blockchains employ other forms of consensus mechanisms like Proof of Stake (PoS), which require a different understanding of cryptographic functions. A broader perspective would help readers appreciate the diversity of crypto technologies.

Another limitation is the relatively brief treatment of hash collisions. The lesson reassures that SHA-256 is highly secure with no known collisions but could have gone further in explaining why older hashing algorithms like MD5 failed and what lessons were learned from their weaknesses. This would provide readers with a more complete understanding of the evolution of cryptographic security and why constant innovation in this space is necessary.

Lastly, while the lesson clearly explains the concept of irreversibility, it overlooks the real-world risks of quantum computing. As quantum technology advances, it may threaten the security of even the strongest cryptographic functions like SHA-256, a topic that could spark deeper critical thinking about the future of blockchain security.


Connections to Cryptocurrency and Blockchain

In the world of cryptocurrencies, cryptographic hashing is not just a tool; it’s a linchpin. The lesson does an excellent job of highlighting how Bitcoin relies on the SHA-256 algorithm to secure its blockchain. However, this technology extends far beyond Bitcoin into other cryptocurrencies and decentralized systems.

Proof of Work (PoW) vs. Proof of Stake (PoS): One of the critical applications of cryptographic hashing is in consensus mechanisms. In PoW systems like Bitcoin, miners compete to solve complex cryptographic puzzles, using hashing functions to verify transactions and add blocks to the blockchain. This process requires immense computational power, which has sparked debates about the environmental impact of mining. In contrast, PoS systems like Ethereum’s upcoming transition to Ethereum 2.0 don’t rely on hashing puzzles to the same extent, offering a more energy-efficient alternative. However, hashing still plays a crucial role in validating transactions and maintaining the integrity of the blockchain.

Smart Contracts and Hashing: Another area where cryptographic hashing comes into play is in smart contracts—self-executing contracts with terms of the agreement directly written into code. In platforms like Ethereum, hashing functions ensure that contract data is secure, immutable, and verifiable on the blockchain. For instance, a smart contract might use a hash to verify that certain conditions have been met before executing a transaction.

Decentralized Finance (DeFi): DeFi platforms, which aim to recreate traditional financial systems using blockchain, also rely heavily on cryptographic hashing. Whether it’s securing lending protocols or verifying decentralized exchanges, hashing ensures that all transactions are transparent and tamper-proof. The lesson’s emphasis on the irreversible and unpredictable nature of cryptographic hashes directly connects to how DeFi platforms maintain their trustless environments, where no central authority is needed to verify transactions.


Broader Implications and Future Outlook

The implications of cryptographic hashing stretch far beyond cryptocurrency. In the broader financial world, cryptography is becoming increasingly essential for securing digital transactions and personal data. As we move further into an era of digital finance, cryptographic functions will likely become more integral in preventing fraud and ensuring the integrity of financial systems.

However, the rise of quantum computing poses a potential threat. Quantum computers, with their ability to process massive amounts of data simultaneously, could theoretically break many current cryptographic systems, including SHA-256. This looming challenge has led to the development of quantum-resistant cryptography, which will be critical for securing both traditional financial systems and blockchain networks in the future.

In terms of blockchain, the move towards more energy-efficient consensus mechanisms like PoS highlights a broader trend in the crypto world: the need for sustainable and scalable solutions. As blockchain adoption grows, so too does the need for hashing algorithms that are both secure and environmentally responsible. The future might see a hybrid approach, where multiple cryptographic functions work together to balance security and efficiency.


Personal Commentary and Insights

From my perspective, the lesson serves as an excellent primer for understanding one of the core technologies behind blockchain. However, what excites me most is how these concepts open up discussions about the future of digital security. As someone deeply involved in both technology and finance, I see cryptographic hashing as a perfect example of how these two worlds intersect. In traditional finance, we’ve long relied on centralized systems to secure data, but with the advent of blockchain, we’re entering a new era of decentralized trust.

Moreover, while the lesson focuses on Bitcoin, I believe the most exciting applications of cryptography are yet to come. In the rapidly expanding DeFi space, hashing ensures that decentralized applications (dApps) can scale while remaining secure. The more we explore the potential of these technologies, the more we realize that cryptographic functions like SHA-256 are just the tip of the iceberg. As we push the boundaries of what’s possible with blockchain, the need for innovative cryptographic solutions will only grow.


Conclusion

Cryptographic hashing may seem like a technical concept, but its impact on the world of finance and technology is profound. From securing Bitcoin transactions to powering the DeFi revolution, hashing functions ensure the integrity and security of decentralized systems. As we look to the future, the continued evolution of cryptographic algorithms will be crucial in addressing new challenges like quantum computing and the demand for sustainable blockchain networks.

This lesson has provided a solid foundation in understanding the magic behind cryptographic hashing, but there’s so much more to explore. Whether you’re intrigued by blockchain’s potential or curious about its security underpinnings, this is just the beginning of your journey. And remember, the Crypto Is FIRE (CFIRE) training program is designed to take you deeper into these essential concepts, equipping you with the knowledge you need to thrive in the crypto world.

Quotes:

  1. “In the world of cryptography, the magic of the black box is real, and it’s known as cryptographic hashing.”
  2. “Hash functions are the backbone of decentralized finance, ensuring every transaction is secure, immutable, and verifiable.”
  3. “As blockchain adoption grows, so too does the need for hashing algorithms that are both secure and environmentally responsible.”

 

 

Magic of Cryptographic Hashing: Power Behind Blockchain Security

In this lesson, we’ll explore the fascinating world of cryptographic hashing functions—a fundamental pillar of blockchain technology. Whether you’re new to cryptocurrencies or have a background in traditional finance, understanding how these functions work is essential to grasp the basics of blockchain, digital transactions, and crypto mining. By the end of this lesson, you’ll see why cryptographic hashes are the secret sauce that keeps Bitcoin and other cryptocurrencies secure. We’ll also discuss how this knowledge fits into the Crypto Is FIRE (CFIRE) training program, equipping you with the skills needed to navigate the crypto landscape.


Core Concepts

  1. Hashing Function

    • Traditional Finance: In traditional systems, hashing is often used for securing passwords or checking data integrity.
    • Crypto Application: In blockchain, it creates unique outputs for transactions, ensuring data immutability.
    • Why It Matters: Hashing is fundamental in securing cryptocurrency networks, like Bitcoin, through its use in mining and validating transactions.
  2. SHA-256

    • Traditional Finance: A cryptographic algorithm used in security protocols like SSL (used in HTTPS websites).
    • Crypto Application: Bitcoin relies on SHA-256 to encrypt transactions and mine new blocks.
    • Why It Matters: SHA-256 helps secure the Bitcoin network, and learning this gives you insight into why Bitcoin mining requires computational power.
  3. Proof of Work (PoW)

    • Traditional Finance: Not a concept in finance but can be related to “work” required to secure contracts.
    • Crypto Application: PoW is the consensus mechanism used in Bitcoin mining to validate transactions.
    • Why It Matters: It’s vital to understand how new blocks are added to a blockchain and how miners are rewarded for their work.
  4. Hash Collisions

    • Traditional Finance: Similar concepts can be found in data security breaches where encryption methods fail.
    • Crypto Application: In hashing functions like SHA-256, hash collisions (two inputs generating the same output) are nearly impossible, ensuring security.
    • Why It Matters: The uniqueness of hashes keeps blockchain data safe from fraud or tampering.
  5. Mining Difficulty

    • Traditional Finance: No direct equivalent, but think of the complexity of verifying high-value transactions.
    • Crypto Application: The mining difficulty adjusts to control how quickly new Bitcoin blocks are mined.
    • Why It Matters: Mining difficulty regulates the issuance of new Bitcoins, keeping inflation in check and securing the network.

Key Sections

1. What Is a Hashing Function?

  • Key Points:
    • A function that takes input and produces a unique output.
    • SHA-256 produces a 64-character output regardless of input size.
    • Hashing is fast and efficient.
  • Explanation: Think of hashing like a magical black box: you put something in (any amount of data), and out comes a specific result (a hash). No matter if you input a letter, a sentence, or an entire book, the output remains the same size—a 64-character combination of letters and numbers. This property is crucial for ensuring data consistency across digital platforms, especially in crypto.
  • Crypto Connection: Hash functions like SHA-256 are the backbone of Bitcoin’s mining process. When miners validate transactions, they create these fixed-length hashes to secure each block.

2. SHA-256: The Workhorse Behind Bitcoin

  • Key Points:
    • SHA-256 stands for Secure Hashing Algorithm, producing 256-bit outputs.
    • It’s highly secure, with no known collisions.
  • Explanation: SHA-256 is a widely trusted cryptographic function, not only because it’s used in Bitcoin but also in many security protocols across the internet. Its robustness lies in the fact that it’s nearly impossible to find two different inputs that produce the same hash (collisions), making it perfect for securing financial transactions.
  • Crypto Connection: Bitcoin uses SHA-256 to encrypt transaction data and produce a unique hash for each block added to the blockchain. Understanding this algorithm is vital for understanding how crypto remains secure.

3. The Magic of Fixed Outputs

  • Key Points:
    • No matter how much data you input, SHA-256 always returns a 64-character hash.
    • It’s quick to compute.
  • Explanation: This property is particularly magical when it comes to large data sets. Whether you input “A” or the entire Encyclopedia Britannica, the output hash is the same size. This consistency is critical for maintaining a fast and scalable blockchain network like Bitcoin.
  • Crypto Connection: This feature enables miners to quickly process transactions without overloading the system, which keeps transaction costs down and blockchain data manageable.

4. Mining Bitcoin: Proof of Work in Action

  • Key Points:
    • Miners must find a hash that meets certain criteria (starts with a specific number of zeros).
    • This process requires immense computational power and guessing.
  • Explanation: Proof of Work (PoW) is the process miners use to secure the blockchain. Miners take a block of transactions, add a random number (called a nonce), and hash the combination. They repeat this process until they find a hash that meets the difficulty level (e.g., a certain number of zeros at the start).
  • Crypto Connection: PoW is why Bitcoin mining is resource-intensive. It requires significant computational power to keep the network secure, which also explains why mining rewards are designed to offset the cost of this energy usage.

5. Why Changing One Letter Changes the Entire Hash

  • Key Points:
    • Tiny changes in input drastically alter the output.
    • This unpredictability ensures security.
  • Explanation: A cryptographic hash is designed so that even the smallest change in input—say, adding an exclamation mark to a sentence—results in a completely different hash. This unpredictability ensures that malicious actors can’t reverse-engineer the input from the hash.
  • Crypto Connection: This property guarantees the integrity of transactions on the blockchain. Even a single alteration to a transaction will produce a wildly different hash, signaling fraud or tampering.

Real-World Applications

  1. Bitcoin Mining Example: When miners validate transactions, they are essentially playing a massive guessing game. They combine transaction data with a nonce and hash it, hoping to find a hash that fits the difficulty requirements (e.g., starts with a certain number of zeros). If successful, they are rewarded with Bitcoin.

  2. Traditional vs. Crypto Use Cases: In traditional finance, hashing is often used to encrypt sensitive data, such as passwords. In crypto, it secures entire blocks of data in the blockchain, ensuring that transactions remain immutable and verifiable.


Challenges and Solutions

Challenges:

  • Mining Difficulty: The increasing difficulty of mining Bitcoin requires more computational resources and electricity.
  • Security Concerns: Some older hashing algorithms, like MD5, have been compromised due to hash collisions.

Solutions:

  • SHA-256: Bitcoin’s use of SHA-256 has proven secure with no known collisions.
  • Alternative Consensus Mechanisms: Newer blockchains, like Ethereum, are moving away from Proof of Work to more energy-efficient methods like Proof of Stake.

Key Takeaways

  1. Hashing Functions Are Essential: Without hashing, blockchain technology as we know it wouldn’t exist.
  2. SHA-256 Powers Bitcoin Mining: This cryptographic algorithm ensures secure transactions and data integrity.
  3. Fixed Outputs Create Scalability: No matter the size of the input, you always get the same size output, allowing blockchain networks to process large amounts of data efficiently.
  4. Proof of Work Provides Security: Bitcoin miners compete to find the correct hash, ensuring the blockchain remains secure.
  5. Small Changes Matter: Even a tiny change in the input drastically changes the hash, making blockchain data immutable.

Discussion Questions and Scenarios

  1. Why is it important for blockchain data to produce fixed-size outputs?
  2. How does SHA-256 differ from traditional encryption methods used in finance?
  3. In what ways could mining become more efficient in the future, and how would this affect the crypto landscape?
  4. Imagine you’re tasked with creating a new cryptocurrency. Would you use Proof of Work or another consensus mechanism? Why?
  5. How does the unpredictable nature of hash outputs contribute to the security of cryptocurrencies?

As you continue with the Crypto Is FIRE (CFIRE) program, the next lesson will build on these foundational principles of cryptographic security and explore how blockchain networks maintain consensus. Stay curious, and see you in the next lesson!


Glossary

  1. Hashing Function: A system that converts input data into a fixed-size string of characters, typically used in cryptography.
  2. SHA-256: The Secure Hashing Algorithm used in Bitcoin.
  3. Proof of Work (PoW): A consensus mechanism requiring miners to perform computational work to validate transactions.
  4. Nonce: A random number used in mining to alter the hash output.
  5. Collision: A situation where two different inputs produce the same hash, which is extremely rare in SHA-256.

This lesson has set a solid foundation for understanding cryptographic hashing and its role in securing cryptocurrency networks. Keep the momentum going as you progress through the CFIRE training program!

 

 

Read Video Transcript
What is a Cryptographic Hash Function? | Where & How It Is Used In Crypto (Animated) 
https://www.youtube.com/watch?v=tO3wHPkeMhE
Transcript:
 Do you know that the passwords you create on most websites are not actually stored on  the website’s servers in a text format? As you may know, many people use the same  password for multiple accounts on many websites. So, your password is stored on a website’s servers,  and then these servers got hacked.
 Then, the hacker can access all your other accounts with  the same password. So, to protect users from hackers, most websites store users’ passwords as hashes.  You may be wondering, what is this hash and how do you log in if your password is stored like this?  Also, what does all of this have to do with crypto? Well, you will get answers to all these  questions in a minute, but first, welcome to CryptoBee where we explain cryptocurrencies  and DeFi topics in the most simple and beginner-friendly way.
 In this video, you will know what is a cryptographic hashing function, some important characteristics  of secure hashing functions, and then we will get to where and how these hashing functions  are used in crypto,, let’s return to our passwords  example.
 Now looking at this hash, you may be thinking that these are some random letters  and numbers. But actually they are not random at all. This hash comes from putting your  password into a hashing function. This hashing function is like a black box that works using  some very complex math to convert any data you give it into a hash, which is simply a series  of letters and numbers.
 You can give it any type of data, a word, an entire book, an image,  or an audio file. Also, it doesn’t matter the length or the size of the data, it will always  give you a hash of the same length. For example, the hash of an entire book is the same length as  the hash of the single letter C. An important point to understand here is that like what we said,  any hash you get is not random at all.
 It is like using math to  summarize the data you give it. So if you input the same data million times into the same hashing  function, you will always get the same hash, it will not change, no matter how many times you try.  Another important thing you need to understand is that cryptographic hash functions are one-way  only. So you can never reverse a hash to get the input data.
 If for example,  we give you this hash, there is nothing you can do to get the input data. You can only try to  guess by trying many many different inputs, and it is still pointless as it is almost impossible  for you to guess the input data that generated a specific hash. By the way, it was hit the like  button if you have been enjoying the video hashed with the SHA-256 hashing function.
 So, returning back to the passwords example, like what we said,  passwords are stored on the server as hashes. So that, even if the server was attacked,  the hacker still won’t be able to know any real passwords. So, how do you log in then?  Well, when you are trying to log in, any password you try will be put into the hashing  function to generate a hash. This new hash will be then compared with the hash stored on the server.
 If the hash generated is the same as the hash stored on the server, then you will successfully  log in. But if the generated hash is different, then you will get the incorrect password message  and you won’t be able to access the account. Now let’s get back to the hashing functions.
 So there are many different types of hashing functions or algorithms like the MD  family, the SHA family, and the RIPE MD family. The SHA-256 is currently the most commonly used  function in crypto as it is still very secure and the SHA-3 function released in 2015 is the  most secure hashing function. But it is slower than the SHA-256  and not supported by many applications and hardware.
 Each hashing function you choose to  use will give you a different hash with a different length for the same input data.  For example, hashes generated with the MD5 hashing function will be composed of 32 letters and  numbers. So, any data you input in MD5, no matter its length  or size, will give you a 32-character hash.
 If you put in a word, it will give you 32 letters  and numbers. And if you put in an entire book, it will still give you 32 letters and numbers.  But on the other hand, the SHA-256, for example, will give you hashes composed of 64 letters and  numbers for any data you give it.  Before we get to how these hash functions are used in crypto, there are two very important  points you need to understand.
 First, you should know that changing anything in the input data  will give you an entirely different hash. For example, if you are hashing a word,  any slight change like replacing a small letter with a capital letter will give you a totally  different hash. If your input data is an image, even changing tiny pixels will give you an entirely different  hash.
 The other important thing you need to understand is that for a hashing function to  be secure, it has to be collision-resistant, which simply means that you can’t find two  different inputs that will give you the same hash. This is very important as hashing functions are  used in digital signatures. If there is a hashing function with two different inputs that will give you the same hash. This is very important as hashing functions are used in digital signatures.
 If there is a hashing function with two different inputs  that can give you the same hash,  then its security is broken and it can be used by attackers  to change the documents or files that you digitally sign on.  For example, a scammer may take a check  that you digitally signed and edit the check amount  and then using some special techniques,  he can still generate the same hash as the original check, which means that your digital signature  is valid on the modified check as well.
 So that is why when collisions are found in a hashing function, we stop using it, as it  is considered insecure and that is what happened with the MD5 and the SHA-1 hashing functions.  A point that may surprise you here is that all cryptographic  hashing functions have collisions, but it should be very, very hard, almost impossible to find them.
 Now, let’s get to where and how these hashing functions are used in crypto.  So, first of all, hashing functions are used to generate your public address  on the Bitcoin and Ethereum blockchains. You may know that your crypto wallet stores your  public key and your private key.
 Your public key is a series of letters and numbers that you  can share with anyone to send you crypto. Your private key is also a series of 64 letters  and numbers that allow you to spend or use the crypto you have, so it should always be  kept private from anyone. What you may not know here is that the wallet address you see and use on Bitcoin or Ethereum  is not your actual public key, but the hashed version of it.
 In Bitcoin, for example,  your public key is hashed with SHA-256, and then the hash we get is hashed again with the ripe MD-160  hashing function. In Ethereum, on the other hand, your public key is hashed one time with the  Keck-Chak-256 function, and then only the last 40 public key is hashed one time with the keck-check 256 function,  and then only the last 40 characters of the hash we get are used as your public key.
 You may be wondering, why do we hash the public keys at the first place?  Well, public keys are hashed to get a shorter version of them that is easier to give to other  users. Some people think that hashing the public keys can help improve security. If the elliptic  curve algorithm was attacked, which is the encryption algorithm we use to  generate our public and private keys.
 Another use of hashing functions is in  hashing transactions on the blockchain. On Bitcoin and Ethereum, all the transactions  are hashed, and the hashes we get act like IDs for these transactions.  So, instead of searching for transaction number 1157 in block  number 114,562, you can get the transaction details by simply searching for its hash,  which is known as transaction ID.
 Still, this is not the only use for transaction hashes or IDs,  as they are also used in generating something called a Merkle tree. Merkle trees allow us to  have a cryptographic  proof of the transactions included in each block and their order. For example, let’s say we have a  Bitcoin block that has only 8 transactions.
 So, we will hash the transactions to get their hashes  or IDs. After that, we will take each pair of sibling transaction hashes and hash them together  again to get 4 new hashes. Then, we will do the same thing with these them together again to get four new hashes.  Then, we will do the same thing with these four hashes to get two new hashes.  Finally, we will hash the last two hashes together to get a new hash called the root hash  or the Merkle root.
 This Merkle root summarizes all the eight transactions included in the block  and it gets included in the header of the block. So each block header in Bitcoin  includes a Merkle root of all the transactions included in the block, which can be up to 2,500  transactions or sometimes more. Any attempts to slightly change or try to reverse previous  transactions in a previous block will totally change the Merkle root, and it will be different  from the one included in in the header, which means that the block will be invalid.
 it will be different from the one included in in the header, which means that the block will be invalid. Merkle trees also allow computers on the Bitcoin network to verify if a specific transaction  was included in a previous block or not. Without downloading all the block’s data,  the block headers alone are enough, which are way smaller in size than the entire block’s data.
 For example, if we want to verify that transaction number 4 was included in this block,  we take the hash of the transaction and try to find the path to the Merkle root.  In this situation, we will need the hash of transaction 3,  so we can combine it with hash 4, we have to get hash 3 4.  We also need hash 1 2 and only the combined hash 5 6 7 8 from the other side.
 Using these hashes, we can get the Merkle root. If we get  the same Merkle root included in the block header, then the transaction was actually included in the  block. Now we can’t talk about hashing functions without talking about proof of work. So in proof  of work, what happens is that computers on the network called miners have to verify transactions  and add new blocks to the blockchain.
 For a block to be  accepted, the miner has to generate a hash that starts with a specific number of zeros.  For example, the network may require the hash to start with five zeros or seven zeros. The more  zeros at the beginning, the more difficult it gets to the miners, and this number changes frequently.  So, a miner will take the block header, which includes the Merkle root and other important information about the block, add to it a random number called the nonce,  and then put all of this into the SHA-256 hashing function to get a hash. If the generated hash
 doesn’t start with the required number of zeros, then the miner has to try again. This process is  known as mining. So, the miner will change the nonce and try again  as many times as it takes to get a correct hash. All the miners on the network are constantly  trying to get a correct hash, and the first miner who actually succeeds will get his block added to  the blockchain and will be rewarded with new bitcoins.
 This search for hashes requires  spending a lot of money on hardware and energy, so it makes trying to add blocks to the blockchain very expensive. This mechanism makes it extremely hard for an attacker to add  his invalid block to the blockchain, as he will need to buy very expensive hardware and run it  for years to get his block rejected at the end by the other computers on the network,  so he will lose all the money he spent.
 Now, when a miner adds a new block to the blockchain,  the correct hash he generated  will be added to the header of the next block. So, all the blocks are connected together in a chain,  where each block header contains the hash of the previous block. So, it is not possible to change  anything or reverse any transactions in a previous block, as slightly changing anything in a previous  block will change its hash, so it will be different from the hash included in the next block.
 Which means that the attacker will need to mine  all the following blocks alone, which is an impossible thing to do, as it requires enormous  computing power that no one can currently buy in the case of Bitcoin or Ethereum.