Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Blockchain Security Fundamentals

Hello! Welcome back to your course on building blockchain applications.

In our last lesson, we explored the big-picture concepts of decentralization, consensus, and immutability. We established that a blockchain is an unchangeable ledger, secured by cryptographically linking blocks together. Today, we're going to zoom in on the specific tools that make this security possible.

This lesson directly addresses how the network is secured by examining its three core cryptographic components:

  • Blocks: The containers for our data.
  • Cryptographic Hashing: The "digital fingerprinting" that ensures data integrity.
  • Public-Key Cryptography: The mechanism for proving ownership and authorizing transactions.

Understanding these technical foundations is essential before we move on to writing code, as they are the building blocks of every secure smart contract and decentralized application.

The Structure of a Blockchain

As the name implies, a blockchain is a chain of blocks. Each block acts as a record book, containing a list of recent transactions. But critically, it also contains two other key pieces of information: a "fingerprint" of its own data and the "fingerprint" of the previous block. This creates an unbreakable link, forming the chain.

The following video provides an excellent visual walkthrough of what a block contains and how these links are formed.

How does a blockchain work - Simply Explained

Watch this segment from the video 'How does a blockchain work' by Simply Explained to see the structure of a block and how blocks are chained together.

Watch from 0:58 to 2:59. Pay close attention to the three elements inside each block: the data, the block's hash, and the hash of the previous block. Notice what happens to the chain when data in an earlier block is altered.

As the video demonstrates, the "fingerprint" used to identify blocks and link them is called a hash. This leads us to our next crucial topic.

Cryptographic Hashing: The Seal of Integrity

At its core, a cryptographic hash function is an algorithm that takes any amount of input data—a single word, a full transaction, or an entire block—and converts it into a fixed-length string of characters. This output string is the hash.

From your backend development experience, you can think of a hash as a perfectly unique and non-reversible ID for a piece of data. If you change even a single bit of the input data, the output hash changes completely and unpredictably. This is known as the avalanche effect.

Hashing Algorithm in Blockchain
This image illustrates how each block in the chain is linked. The 'Prev Hash' of Block 2 is the 'Hash' of Block 1. This dependency ensures that if Block 1's data were altered, its hash would change, breaking the link and invalidating the entire subsequent chain.

For a hash function to be secure and useful in a blockchain, it must have several key properties.

What It Is and Why It Is Essential in Blockchain

This article from Gemini provides a clear and concise explanation of the essential properties of a cryptographic hash function.

Read the sections 'What Is a Hash Function in Blockchain?', 'What Are the Key Characteristics of Cryptographic Hash Functions?', and 'Why Are Hash Functions Essential in Blockchain?'. Focus on understanding these five properties: Deterministic Fast Pre-image resistant (one-way) Avalanche effect Collision resistant

These properties are what give the blockchain its immutability. Because each block's hash is included in the next, altering an old block would change its hash, causing a mismatch in the subsequent block. This creates a domino effect that would require an attacker to recalculate every single block that came after, a computationally impossible task on a decentralized network.

It's also worth noting that different blockchains use different hashing algorithms. Bitcoin famously uses SHA-256, while Ethereum, the platform we'll be building on, uses Keccak-256.

Public-Key Cryptography: Who Owns the Assets?

We now understand how the ledger's history is protected from modification. But how does the network know who has the right to initiate new transactions? How do you prove you own the crypto you want to send?

This is solved using asymmetric cryptography, also known as public-key cryptography. This system provides each user with a pair of mathematically linked keys:

  1. Public Key: This is used to derive your blockchain address (like an account number). It is public and you can share it with anyone who wants to send you assets.
  2. Private Key: This is a secret password that you must never share. It is used to "sign" transactions, creating a digital signature. This signature serves as proof that you, and only you, authorized the transaction from your account.
Public Key Cryptography: Encryption and Digital Signatures
This diagram shows the two primary uses of public-key cryptography. For privacy, a message is encrypted with the receiver's public key. For authentication, a sender signs a message with their own private key, and anyone can verify it with their public key.

The video below explains how these keys and digital signatures work together to secure transactions in a blockchain context.

Introduction to Cryptography in Blockchain Explained | Blockchain Cryptography

This segment explains how public/private key pairs and digital signatures are used to authenticate transactions and manage user addresses on the blockchain.

Watch the video starting at 2:00, focusing on the descriptions of 'asymmetric key cryptography' and how it's applied in cryptocurrencies (from 5:45 to 8:23). Pay attention to the role of the private key in authorizing actions and the public key in defining a user's address.

In essence, a digital signature mathematically proves three things:

  • Authenticity: The transaction was created by the holder of the private key.
  • Integrity: The transaction details (amount, recipient) have not been changed since it was signed.
  • Non-repudiation: The sender cannot later deny having sent the transaction.

How It All Comes Together

Let's recap how these three elements—blocks, hashing, and public-key cryptography—combine to create a secure network:

  1. A user decides to send cryptocurrency. They create a transaction message and sign it with their private key.
  2. The signed transaction is broadcast to the network. Nodes use the sender's public key to verify the signature is valid.
  3. Verified transactions are collected by a node and bundled into a new block.
  4. The node calculates the hash of this new block. The block also includes the hash of the most recent block on the chain, locking it into place.
  5. Through a consensus mechanism (which we'll cover next), the network agrees to add this new block to the end of the chain, making the transactions within it a permanent part of the public record.

Any attempt to subvert this system fails. To send funds from your account, an attacker needs your private key. To alter a past transaction, they need to break the hash chain and overpower the entire network's consensus. This multi-layered security is what makes blockchain so robust.

Test your understanding!

An attacker wants to change a transaction in a block that was confirmed an hour ago. They want to redirect the funds to their own address. What two cryptographic hurdles make this practically impossible? Explain the role of each.

Show answer

The two main cryptographic hurdles are:

  1. The Hash Chain (Immutability): The transaction is inside a block that has a specific hash. This hash is included in the next block, which has its own hash included in the block after that, and so on. By changing the transaction details, the attacker changes the block's data, which would result in a completely different hash. This breaks the chain, as the new hash would no longer match the "previous hash" pointer in the next block. The attacker would have to re-calculate the hashes for all subsequent blocks, which is computationally infeasible.

  2. Digital Signatures (Ownership): The original transaction was signed with the legitimate sender's private key. Even if the attacker could somehow create a new block with the altered transaction, they cannot fake the digital signature. Without the original sender's private key, they cannot create a valid signature for the fraudulent transaction that sends funds to their address. Other nodes on the network would immediately reject this transaction as invalid during verification because the signature wouldn't match the sender's public key.

Conclusion

In this lesson, we dissected the cryptographic engine that powers a secure blockchain. You now have a solid, technical understanding of how the system works under the hood.

Key Takeaways:

  • A blockchain is a sequence of blocks, where each block contains transaction data, its own hash, and the hash of the previous block.
  • Cryptographic hashing creates a unique, fixed-size "fingerprint" for data. Its properties (especially the avalanche effect and pre-image resistance) make the chain of blocks tamper-evident and immutable.
  • Public-key cryptography provides each user with a public key (for receiving) and a private key (for signing). Digital signatures prove ownership and authorize transactions, ensuring only the rightful owner can move assets.

These three components work in unison to create a system that is both transparent and secure, without relying on a central authority.

Preview of the Next Lesson:
We've seen how blocks are secured and linked, but how does the network decide who gets to add the next block? This is the job of a consensus mechanism. In our next lesson, we will compare the two most prominent approaches: Proof-of-Work (PoW), the original mechanism used by Bitcoin, and Proof-of-Stake (PoS), the more energy-efficient system that Ethereum now uses.

Can't find a good explanation? Sign up and we'll make it for you

Sign up