Understanding the concept of the Nonce & SHA3-256 in a Blockchain with NodeJS
Tags: node, blockchain, tutorial
Published: 2020-08-03 08:59:33

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](https://emn178.github.io/online-tools/sha3_256.html)  , 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
```
**False** - The hash don't start with '000'
```
'Hello, World1'
940fa6081f74fff0aab240034da8dc7569970a28ddfb63af61d62c746484a2b1
```
**False** - The hash don't start with '000'
```
'Hello, World2'
0b78c66e6596ce0ea1af4e3fab848663ef3b0c4b6bc771dd2338d89cacc1bc52
```
**False** - The hash don't start with '000'
'Hello, World3'
```
f2d8cec2539f4a237f5099c6a68209329873668b694634d780e289c994229389
```
**False** - The hash don't start with '000'
[...]
```
'Hello, World567'
000f96a254d94fb611f76bf639906b587c8818441e180d3dd1ea339a39171d2c
```
**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

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

To make it easier for us, we need to install the [sha3 module](https://yarnpkg.com/package/sha3)


```bash
yarn add sha3
```

We can now update our index.js

```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
```

To start the miner:
```bash
nodemon index.js
```