Tugas4 - BC - Hilmy Bagas Prasetyo - 2C - 2310631160061

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

3/19/24, 11:58 PM Untitled0.

ipynb - Colaboratory

from hashlib import sha256


import json

teks = "Halo Nama Saya Hilmy"

encrypt = hashlib.sha256(teks.encode()).hexdigest()

print(encrypt)

2f179fa67f07b5d39a8ff5de47c16debc8262e3ccad349631e3122c96eb2a782

blok = {
'Header' : 'blok Hilmy',
'Previous Block Adress': '0000',
'nounce':0,
'data': 'test 123'
}

encrypt = sha256( json.dumps(blok,sort_keys=True).encode('utf-8') ).hexdigest()


print(encrypt)

2e29739903c9ce3100156558c2a7c45414f9e7799c4fb1939c8d70b721534fae

teks ='0'*64
print(teks)
print(len(teks))

0000000000000000000000000000000000000000000000000000000000000000
64

create block add new block into blockchain

https://colab.research.google.com/drive/1FUip8EDOKqJe4nzC0HprksFJfg-Q826d#scrollTo=d_YfjsoMpNZB&printMode=true 1/3
3/19/24, 11:58 PM Untitled0.ipynb - Colaboratory

class blockchain:
def __init__(self):
self.chain = []

def createBlock(self,data):
blok = {}
if self.length() < 1:
blok['Header'] = 'blok Hilmy'
blok['Previous adress'] = '0'*64
blok['data'] = data
blok['nounce'] = 1

else:
blok['Header'] = 'blok Hilmy' + str(self.length() + 1)
blok['Previous adress'] = self.getAdress(self.chain[-1])
blok['data'] = data
blok['nounce'] = 0

while self.getAdress(blok)[0:2] != '00':


blok['nounce'] = blok['nounce'] + 1

return blok

def getAdress(self,blok):
return sha256( json.dumps(blok,sort_keys=True).encode('utf-8') ).hexdigest()
def addBlock2Chain(self,block):
self.chain.append(blok)
def length(self):
return len(self.chain)

bc = blockchain()

blok = bc.createBlock({'user':'Hilmy','loan':500000})

blok

{'Header': 'blok Hilmy3',


'Previous adress': '001447b6536ca111675316d3845fb921524d0d177037ae521b82b2c8381692b6',
'data': {'user': 'Hilmy', 'loan': 500000},
'nounce': 3}

bc.getAdress(blok)

'0049dfecb40488512e40cbdd2562a43670167e14064350b99c5c0e5f3d1b7c9d'

bc.addBlock2Chain(blok)

https://colab.research.google.com/drive/1FUip8EDOKqJe4nzC0HprksFJfg-Q826d#scrollTo=d_YfjsoMpNZB&printMode=true 2/3
3/19/24, 11:58 PM Untitled0.ipynb - Colaboratory

bc.length()

bc.chain

[{'Header': 'blok Hilmy',


'Previous adress':
'0000000000000000000000000000000000000000000000000000000000000000',
'data': {'user': 'Hilmy', 'loan': 200000},
'nounce': 588},
{'Header': 'blok Hilmy2',
'Previous adress':
'0084683f8b033a22e509eca1e91544b62c8af0c4a5633c0a7ff490c4d5cacd2c',
'data': {'user': 'Hilmy', 'loan': 100000},
'nounce': 125},
{'Header': 'blok Hilmy3',
'Previous adress':
'001447b6536ca111675316d3845fb921524d0d177037ae521b82b2c8381692b6',
'data': {'user': 'Hilmy', 'loan': 500000},
'nounce': 3}]

bc.getAdress(bc.chain[-1])

output '0049dfecb40488512e40cbdd2562a43670167e14064350b99c5c0e5f3d1b7c9d'

https://colab.research.google.com/drive/1FUip8EDOKqJe4nzC0HprksFJfg-Q826d#scrollTo=d_YfjsoMpNZB&printMode=true 3/3

You might also like