DEV Community

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

Posted on

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

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:

844af7bf6a5d414359dcd8845cb52d515397410e1668e00c8469ea8728c4ffe8

If you add a simple '.' in the end, you'll end up with a completly different hash :
3d3a78714924f9574b7dae02399feb0bf100c3893ed1f7a6934a687ff088f7d4
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'
44ede83c11bee0db1e9c53ae734c705771f56dff82413811c70e0f39862f3e7c
Enter fullscreen mode Exit fullscreen mode

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

'Hello, World1'
940fa6081f74fff0aab240034da8dc7569970a28ddfb63af61d62c746484a2b1
Enter fullscreen mode Exit fullscreen mode

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

'Hello, World2'
0b78c66e6596ce0ea1af4e3fab848663ef3b0c4b6bc771dd2338d89cacc1bc52
Enter fullscreen mode Exit fullscreen mode

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

f2d8cec2539f4a237f5099c6a68209329873668b694634d780e289c994229389
Enter fullscreen mode Exit fullscreen mode

False - The hash don't start with '000'
[...]

'Hello, World567'
000f96a254d94fb611f76bf639906b587c8818441e180d3dd1ea339a39171d2c
Enter fullscreen mode Exit fullscreen mode

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:
https://nodejs.org/en/

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
Enter fullscreen mode Exit fullscreen mode

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

yarn add sha3
Enter fullscreen mode Exit fullscreen mode

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
  hash.reset();
  // 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
    nonce++;
  }
}

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

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

To start the miner:

nodemon index.js
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
thomasradulesco profile image
Thomas-Radulesco

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

Collapse
 
icesofty profile image
Florian • Edited

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

Collapse
 
kalem137 profile image
kalem137

Hello Florian,

thank very much you for this very valauable tutorial.

Is it possible to write a similar program for SHA2(SHA256)
in order to found a nonce for SHA2(SHA256)?

In this tutorial is SHA3-256 used.

I have added sha2 module with yarn and tried to achive this but sha2

module doesn't support some methods e.g. like digest and update etc.

Best Regards
kalem

Collapse
 
manomite profile image
Adeyeye George

This is exactly what have been looking for...

Thanks for this... If you have another tutorial to validate a nonce please I will be glad to check them...