Blockchain for Security

Abhiroop Bas
3 min readApr 27, 2021

--

Blockchain Structure

Introduction to Blockchain

Courtesy: Gary Fox

The Blockchain uses the methodology of creating a distributed network of chain of blocks that are connected to the immediate previous block. A typical block consists of 3 major components: data, previous hash code and the block hash code. The hash is a unique encrypting technique that acts as a link between the different blocks. The different methods that can be leveraged gives it opportunity to make changing of data of any of the block almost impossible.

This technology is further leveraged to ensure irreplaceable data can be secured such that they cannot be changed after creation. This can be used to protect crucial documents and data in the digital age.

Code

import datetime

import hashlib

class Block:

blockNo = 0

data = None

next = None

hash = None

nonce = 0

previous_hash = 0x0

timestamp = datetime.datetime.now()

def __init__(self, data):

self.data = data

def hash(self):

h = hashlib.sha256()

h.update(

str(self.nonce).encode(‘utf-8’) +

str(self.data).encode(‘utf-8’) +

str(self.previous_hash).encode(‘utf-8’) +

str(self.timestamp).encode(‘utf-8’) +

str(self.blockNo).encode(‘utf-8’)

)

return h.hexdigest()

def __str__(self):

return “Block Hash: “ + str(self.hash()) + “\nBlockNo: “ + str(self.blockNo) + “\nBlock Data: “ + str(self.data) + “\nHashes: “ + str(self.nonce) + “\n — — — — — — — “

class Blockchain:

diff = 15

maxNonce = 2**32

target = 2 ** (256-diff)

block = Block(“Genesis”)

dummy = head = block

def add(self, block):

block.previous_hash = self.block.hash()

block.blockNo = self.block.blockNo + 1

self.block.next = block

self.block = self.block.next

def mine(self, block):

for n in range(self.maxNonce):

if int(block.hash(), 16) <= self.target:

self.add(block)

print(block)

break

else:

block.nonce += 1

blockchain = Blockchain()

ch=’y’

while(ch==’y’):

print(“Enter your data into a secure database”)

n=input(“Enter name of receiver: “)

a=input(“Enter amount of Money: “)

print(“Enabling transaction…”)

data=n+” received Rs. “+a

blockchain.mine(Block(data))

ch=input(“Press y to add more: “)

print(“Passbook”)

while blockchain.head != None:

print(blockchain.head)

blockchain.head = blockchain.head.next

The simple code uses a blockchain infrastructure to store bank details. This can further be extended to store sensitive information on a wider scale.

Output

Enter your data into a secure database
Enter name of receiver: Mr. Basak
Enter amount of Money: 456
Enabling transaction…
Block Hash: 54348deebaf047e26e2c41c6628fe3a3d4d838e7b67fd3d51bd56a834adf66cd
BlockNo: 1
Block Data: Mr. Basak received Rs. 456
Hashes: 35090
— — — — — — —
Press y to add more: y
Enter your data into a secure database
Enter name of receiver: Mr. Sen
Enter amount of Money: 235
Enabling transaction…
Block Hash: fb6f599ae3d0e69fe8e7b69c074c0b66935183289adaa99215733a5c9dc2f0cc
BlockNo: 2
Block Data: Mr. Sen received Rs. 235
Hashes: 39214
— — — — — — —
Press y to add more: y
Enter your data into a secure database
Enter name of receiver: Mrs. Yoda
Enter amount of Money: 399
Enabling transaction…
Block Hash: 8453305443e69009e37bd60bb28e1fae72d39022fda3dbf3c7261bee116086ea
BlockNo: 3
Block Data: Mrs. Yoda received Rs. 399
Hashes: 46731
— — — — — — —
Press y to add more: y
Enter your data into a secure database
Enter name of receiver: Ms.Jain
Enter amount of Money: 100
Enabling transaction…
Block Hash: 7d057d09b1a5eb3b1e1366a628369ca514ce9c3a9543be54765e5e8a88de2145
BlockNo: 4
Block Data: Ms.Jain received Rs. 100
Hashes: 9551
— — — — — — —
Press y to add more: n
Passbook
Block Hash: cb08655cd76f4cc9b72502c074005c8401c94035e9b8ee8a2b5b3af8ca90430c
BlockNo: 0
Block Data: Genesis
Hashes: 0
— — — — — — —
Block Hash: 54348deebaf047e26e2c41c6628fe3a3d4d838e7b67fd3d51bd56a834adf66cd
BlockNo: 1
Block Data: Mr. Basak received Rs. 456
Hashes: 35090
— — — — — — —
Block Hash: fb6f599ae3d0e69fe8e7b69c074c0b66935183289adaa99215733a5c9dc2f0cc
BlockNo: 2
Block Data: Mr. Sen received Rs. 235
Hashes: 39214
— — — — — — —
Block Hash: 8453305443e69009e37bd60bb28e1fae72d39022fda3dbf3c7261bee116086ea
BlockNo: 3
Block Data: Mrs. Yoda received Rs. 399
Hashes: 46731
— — — — — — —
Block Hash: 7d057d09b1a5eb3b1e1366a628369ca514ce9c3a9543be54765e5e8a88de2145
BlockNo: 4
Block Data: Ms.Jain received Rs. 100
Hashes: 9551
— — — — — — —

More security features may be added as per requirement. The above structure is an effective method of storing information more specifically in glacial form.

--

--