Tugas4 - BC - Hilmy Bagas Prasetyo - 2C - 2310631160061
Tugas4 - BC - Hilmy Bagas Prasetyo - 2C - 2310631160061
Tugas4 - BC - Hilmy Bagas Prasetyo - 2C - 2310631160061
ipynb - Colaboratory
encrypt = hashlib.sha256(teks.encode()).hexdigest()
print(encrypt)
2f179fa67f07b5d39a8ff5de47c16debc8262e3ccad349631e3122c96eb2a782
blok = {
'Header' : 'blok Hilmy',
'Previous Block Adress': '0000',
'nounce':0,
'data': 'test 123'
}
2e29739903c9ce3100156558c2a7c45414f9e7799c4fb1939c8d70b721534fae
teks ='0'*64
print(teks)
print(len(teks))
0000000000000000000000000000000000000000000000000000000000000000
64
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
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
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
bc.getAdress(bc.chain[-1])
output '0049dfecb40488512e40cbdd2562a43670167e14064350b99c5c0e5f3d1b7c9d'
https://colab.research.google.com/drive/1FUip8EDOKqJe4nzC0HprksFJfg-Q826d#scrollTo=d_YfjsoMpNZB&printMode=true 3/3