Thuta Learning
ExercisesSecurityadvanced

Blockchain Exercises — Set 1 (Hashing & Block Basics)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Practice Blockchain Exercises — Set 1 (Hashing & Block Basics) on your own
  • Reinforce the skills you've already learned through practice
  • Get comfortable finding and fixing your own mistakes

Let's think about this for a second

This lesson doesn't teach anything new — it's about writing your own code to get hands-on with the hashing, blocks, and immutability ideas already explained in the Basic chapter. The goal is to actually see SHA256's avalanche effect (a tiny change in input completely changes the output). The tasks are short — you can open node in the terminal and run them directly.

Exercises

Task 1 — Require Node.js's crypto module, hash the two strings 'Hello Blockchain' and 'hello Blockchain' (differing only by the capital/lowercase H) with SHA256, and compare the outputs. Task 2 — Write a genesis block object yourself containing {index:0, timestamp: Date.now(), data:'Genesis Block', previousHash:'0'}, then combine it with JSON.stringify() and compute its SHA256 hash. Task 3 — Change the data field of the block object you wrote in Task 2, recalculate the hash, and compare it against the original hash to see whether they match.

Code Example

javascript
const crypto = require('crypto');

function sha256(input) {
  return crypto.createHash('sha256').update(input).digest('hex');
}

// Task 1: string နှစ်ခု hash ယှဉ်ပါ (H စာလုံးကြီး/သေးပဲ ကွာပါတယ်)
const hashA = sha256('Hello Blockchain');
const hashB = sha256('hello Blockchain');
console.log('hashA:', hashA);
console.log('hashB:', hashB);

// Task 2: genesis block object ရေးပြီး hash တွက်ပါ
const genesisBlock = {
  index: 0,
  timestamp: Date.now(),
  data: 'Genesis Block',
  previousHash: '0',
};
const genesisHash = sha256(JSON.stringify(genesisBlock));
console.log('genesisHash:', genesisHash);

// Task 3: data ပြောင်းပြီး hash ကို ပြန်တွက်ကြည့်ပါ (TODO — ကိုယ်တိုင်ရေးပါ)
// const changedBlock = { ...genesisBlock, data: 'Genesis Block v2' };
// const changedHash = sha256(JSON.stringify(changedBlock));
// console.log('changedHash:', changedHash);
You should see
Once you finish all three tasks, you'll see three hash strings in the terminal: two completely different hashes for strings that differ by just one character, and a block's hash.

5-Minute Try-It

Paste your hash outputs into a note and spend 5 minutes observing and writing down which character positions changed by how much.

A Quick Word of Caution

You can't eyeball SHA256 output and guess how similar two inputs were — even a one-character difference makes the output completely unrecognizable, so it's much easier to understand by doing a line-by-line diff.

Easy traps

  • Passing an object straight into hash.update(data) without calling JSON.stringify() first, resulting in an [object Object] error or wrong hash
  • Printing the raw Buffer object instead of calling .digest('hex') first, making the output unreadable

Now Try It Yourself

Paste your hash outputs into a note and spend 5 minutes observing and writing down which character positions changed by how much.

You'll know it worked when: Once you finish all three tasks, you'll see three hash strings in the terminal: two completely different hashes for strings that differ by just one character, and a block's hash.

Blockchain Exercises — Set 1 (Hashing & Block Basics) | Thuta Learning