Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Code for single block

import hashlib
import datetime
index="0"
print("index is = {}".format(index))
import time
timestamp=time.time()
print("Timestamp is = {}".format(timestamp))
previoushash="0"
print("Previous hash is = {}".format(previoushash))
import hashlib
A=input(str("enter the string or data = "))
print("Data is = {}".format(A))
hash_object = hashlib.sha256(A.encode())
currhas=(hash_object.hexdigest())
print("current hash is = {}".format(currhas))
Output of the single block
1/1
Code for blockchain in autonomous vehicle

import hashlib
import datetime
import json
class block():
def __init__(self):
self.index = input("allocate the index = ")
import time
self.tstamp = time.time()
self.data = input("enter the command = ")
self.prevhash = '0'
self.hash = self.calculate()
print("enter data for next block")
def calculate(self):
block_string= json.dumps({"index"
:self.index,"tstamp":self.tstamp,"data":self.data,
"prevhash":self.prevhash},
sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
def show(self):
string="index: " + str(self.index) + "\n"
string+= "timestamp: " + str(self.tstamp) + "\n"
string += "transection: " + str(self.data) + "\n"
string += "prevhash: " + str (self.prevhash) + "\n"
string += "current_hash: " + str (self.hash) + "\n"
return string

class blockchain():
def __init__(self):
self.chain = [self.genrategennises(),]
def genrategennises(self):
return block()
def getlast(self):
return self.chain[-1]
def addblock(self,newblock):
newblock.prevhash = self.getlast().hash
newblock.hash = newblock.calculate()
self.chain.append(newblock)
def ischainvalid(self):
for i in range(1,len(self.chain)):
prevb = self.chain[i-1]
currb = self.chain[i]
if(currb.hash != currb.calculate()):
return False
if(currb.prevash != prevb.hash):
return False

vehicle = blockchain()
vehicle.addblock(block())
vehicle.addblock(block())
vehicle.addblock(block())
for b in vehicle.chain:
print(b.show())

Output of blockchain in autonomous vehicle


1/3
Continue 2/3
Continue 3/3

You might also like