2.区块链信息的不可篡改

体验一下信息的不可篡改,hash或数据一旦修改,就会出现连不上与检验不通过。

NiuMessage.py

import hashlib
import datetime
from Transcation import Transcation

class NiuMessage:

    def __init__(self,transcation):
        self.transcation = transcation
        self.hash = None
        self.pre_hash = None
        self.timestamp = datetime.datetime.now()
        self.payload_hash = self._hash_payload() #交易后的hash

    def _hash_payload(self): #对交易时间与数据进行hash
        return hashlib.sha256( (str(self.timestamp) + str(self.transcation)).encode("utf-8") ).hexdigest()

    def _hash_message(self): #将前一个hash与交易后的hash,再hash
        return hashlib.sha256( (str(self.pre_hash) + str(self._hash_payload())).encode("utf-8") ).hexdigest()

    def seal(self): #就是将交易hash,设置为现在的hash,这里为什么不能赋值在构造器,因为构造器不能调用两个函数
        self.hash = self._hash_message()

    def __repr__(self):
        mystr = "hash:{} , pre_hash:{} , data:{}".format(self.hash,self.pre_hash,self.transcation)
        return mystr

    def validate(self,Message): #上个交易的hash与数据要能对上
        if (Message.hash != self.pre_hash):
            raise InvalidateMessage("交易hash被篡改 ," + str(self))
        if(Message.hash != Message._hash_message()):
            raise InvalidateMessage("交易数据被篡改 ,"+str(self))

    def link(self,Message):
        self.pre_hash = Message.hash

class InvalidateMessage(Exception):
    def __init__(self,*args,**kwargs):
        Exception.__init__(self,*args,**kwargs)

if __name__ == "__main__":
    t1 = Transcation("I", "him", 10)
    t2 = Transcation("him", "it", 10)
    t3 = Transcation("it", "her", 10)

    m1 = NiuMessage(t1)
    m1.seal()

    m2 = NiuMessage(t2)
    m2.link(m1)
    m2.seal()
    m2.validate(m1)

    m3 = NiuMessage(t3)
    m3.link(m2)
    m3.seal()
    m3.validate(m2)

    print(m1)
    print(m2)
    print(m3)

    #m2.hash="33" 
    #m2.transcation = "fda"
    #m3.validate(m2)

Transcation.py

import datetime

class Transcation:
    def __init__(self,payer,recer,money):
        self.payer = payer
        self.recer = recer
        self.money = money
        self.timestamp = datetime.datetime.now()

    def __repr__(self):
        return str(self.payer) + " pay " + str(self.recer) + " " + str(self.money) + " " + str(self.timestamp)

去掉上面的注释,即可感受到连不上与校验错误。

你可能感兴趣的:(2.区块链信息的不可篡改)