Cover image for Understanding the concept of the Nonce & SHA3-256 in a Blockchain with NodeJS

Understanding the concept of the Nonce & SHA3-256 in a Blockchain with NodeJS

icesofty profile image Florian ・3 min read

The SHA3-256 algorithm will convert your data (text, images... ) in a hash. The beauty of this algorithm is that it's unpredictable and irreversible (you can't find the initial data only with the hash)

Let's take the following text example: 'Hello, World'
If you enter 'Hello, World' here , you'll get the following hash:


If you add a simple '.' in the end, you'll end up with a completly different hash :
There is no predictible pattern behind SHA3-256, and it makes it nearly impossible to hack.

It's basically what a blockchain does; it hashes block data.
BUT to validate a block, you'll need to find the 'secret key.'
This secret-key is called a nonce; a nonce is a number that added to the block will make the hash start with the number of 0 sets in difficulty.

Let's take a simple example:

We have the following text in our block: 'Hello, World'
We set the difficulty to '000' - That means that we want the hash to start with '000.'
To validate the block, we need to found a nonce to the data that will make the hash starts with the set difficulty.
We start with nonce = 0

'Hello, World0'

False - The hash don't start with '000'

'Hello, World1'

False - The hash don't start with '000'

'Hello, World2'

False - The hash don't start with '000'
'Hello, World3'


False - The hash don't start with '000'

'Hello, World567'

True - The hash start with '000'

The more 0 you add to the difficulty, the harder it will become to find the nonce.
And because we're devs, we'll create a little NodeJS project to 'mine' the block for us.

Let's code!

To code this miner, I'll assume that you already have NodeJS, Nodemon & Yarn installed on your machine.
To install NodeJS & NPM:

To install yarn with npm:
npm install -g yarn

To install Nodemon globally:
npm install -g nodemon

First, create a folder & the entry point of our app

mkdir Sha256-demo
cd Sha256-demo
touch index.js
yarn init

To make it easier for us, we need to install the sha3 module

yarn add sha3

We can now update our index.js

// We import the module in the project
const { SHA3 } = require("sha3");

// We create a new SHA3-256 instance
const hash = new SHA3(256);

//The initial text data
let words = "Hello, World";
//We set the difficulty
let difficulty = "000";
// nonce start at 0
let nonce = 0;
// The final hash will be updated in this var
let hex = "";
// Our switch
let check = true;

// We loop until we found the nonce
while (check) {
  // We reset the hash on each loop
  // We add the sentence to hash
  hash.update(words + nonce.toString());
  // We digest the hash in hex
  let digestHex = hash.digest("hex");
  // We chack if the digest start with the difficulty
  if (digestHex.startsWith(difficulty)) {
    // if true, we store the hex
    hex = digestHex;
    // We turn of our switch to end the loop
    check = false;
  } else {
    // if false, we increment the nonce and start again the process

console.log("Nonce: " + nonce); 
// Nonce: 567
console.log("Hex: " + hex); 
// Hex: 000f96a254d94fb611f76bf639906b587c8818441e180d3dd1ea339a39171d2c

// This means that the SHA3-256 of 'Hello, World' is 000f96a254d94fb611f76bf639906b587c8818441e180d3dd1ea339a39171d2c

To start the miner:

nodemon index.js

Posted on by:

icesofty profile



Hey, I'm a Junior Full Stack dev. https://oclock.io/ alumn (French) I love Golang - JS - NodeJS


Editor guide

I don't get it. Now we have the nonce, what can we do with it ? How / where can you "validate" our blockchain that starts with "000" ? Thanks


Hey Thomas,
Thanks for your feedback. This is just a simple introduction to understand the concept of the nonce, not a full step-by-step Blockchain tutorial ;)

I'll make other tutorials to validate and register a block in a blockchain ASAP.

Anyway, you can also check this repo I've made some months ago on Glitch: glitch.com/~oracle-blockchain
It's a fully working Blockchain made with Go & NodeJS.

[UPDATE] Here is the article to validate & add a block in the Blockchain! Enjoy