python实现区块链代码

如果你明白了原理其实挺简单的。

加密算法是python自带的

需要导入hashlib

import hashlib as hash

sha = hasher.sha256()

sha.update('your content')

print sha.hexdigest()

输出:baca6a6db216faf43b107e5f00e20eaf22edc75e922de5ccc08c16b91b9eb3bd

如果内容变成(索引+时间戳+内容+上次加密的hash内容)这个没有问题吧

然后创建一个类,把这些内容保存起来,放入到列表里。

下面贴出源代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import hashlib as hasher
import datetime as date

class Block:
    def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.hash_block()

    def hash_block(self):
        sha = hasher.sha256()
        sha.update(str(self.index) +
                   str(self.timestamp) +
                   str(self.data) +
                   str(self.previous_hash))
        return sha.hexdigest()

def create_genesis_block():
  return Block(0, date.datetime.now(), "Block Data", "0")

def next_block(last_block):
  this_index = last_block.index + 1
  this_timestamp = date.datetime.now()
  this_data = "Hey! I'm block " + str(this_index)
  this_hash = last_block.hash
  return Block(this_index, this_timestamp, this_data, this_hash)


blockchain = [create_genesis_block()]
previous_block = blockchain[0]

#生成20个为例
num_of_blocks_to_add = 20

for i in range(0, num_of_blocks_to_add):
    block_to_add = next_block(previous_block)
    blockchain.append(block_to_add)
    previous_block = block_to_add
    # Tell everyone about it!
    print "Block #{} has been added to the blockchain!".format(block_to_add.index)
    print "Hash: {}\n".format(block_to_add.hash)

结果:

Block #1 has been added to the blockchain!

Hash: b871f17f63685be10f35820bb380f53aabc2ffeed683a7d6de9787194391b1a0

Block #2 has been added to the blockchain!
Hash: df74f5d23e0772a281a0ffbc0802e4f84abcefc6be59d8af0813413d322b8e68

Block #3 has been added to the blockchain!
Hash: e9cad108bbd80eafa33d61e9cd10a37f9c5ccacac6b1293a9e0b0d3648d1d343

Block #4 has been added to the blockchain!
Hash: 10ad66a24dfa08d52034f3c366d49634cc9b1e3e614d13de9be41eed550838e6

Block #5 has been added to the blockchain!
Hash: 7d96771e2c1ef0721aca7ceb1a599550bc33d07020c419e4c1513e4f8d420a13

Block #6 has been added to the blockchain!
Hash: 08f7e29c8c9641705caa15deab28db75dd5dd66d8d98b7eb5187f40ce31dee65

Block #7 has been added to the blockchain!
Hash: e624c681afc1f6f2e785b89275bce8f5c1ac3e5b94c34ac7a0363dbdc76da41a

Block #8 has been added to the blockchain!
Hash: 6d3129403393864ec54df6e94ddfe72d6efed98383c362eedf51a0548f0f9d74

Block #9 has been added to the blockchain!
Hash: 141202a42c71ff911a829df5685737eba74d008304113381fe1fca6b3d9217be

Block #10 has been added to the blockchain!
Hash: b45029d2a40f5d691d2ce871bb7ac7d4aabab8a766349a9996c9cec07a7f2450

Block #11 has been added to the blockchain!
Hash: e24c5eefb57fe754a8f75b4b17c7d17e3fdcb8efb0713ba8ec57270d4321b139

Block #12 has been added to the blockchain!
Hash: df445b248db7b0540fbae61773a925323cccb072126a126aaf178800eca1d683

Block #13 has been added to the blockchain!
Hash: 0ffa6e5b54d2bc738afe636fd253f4afd7b13995f59ac43b992d10944f0da934

Block #14 has been added to the blockchain!
Hash: 4d45a38b7b10267c195efe8371b26e825018c32db5e2d24f174388798fedf35f

Block #15 has been added to the blockchain!
Hash: 7caae5e46a187481534f870a2fb39f6f1169162db9264273b4376665925d4d7f

Block #16 has been added to the blockchain!
Hash: 54770c9fff28e34218663812cf3234cb390715cbc24b85df236d2bb0e1e88cd5

Block #17 has been added to the blockchain!
Hash: 8a3ae9c8599c6663e6171ebca9ec6a94a1629d73a2ef91ead27447327bc741b8

Block #18 has been added to the blockchain!
Hash: 09f6c1e7b4b7a5ffee15929605c365054671447a84cbf2a0e326d43004c74ad4

Block #19 has been added to the blockchain!
Hash: 64d38c2df1190b24f68127c9d6158e1aa23c6edec0baf3280245befbbc104e7c

Block #20 has been added to the blockchain!
Hash: 4387beb245f1bb48938da280416ab5c21f17623377dd67915d6441ea47385899

posted @ 2018-02-28 17:28  北京流浪儿  阅读(2374)  评论(0编辑  收藏  举报