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

加密算法是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", "") 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

python实现区块链代码的更多相关文章

  1. python搭建区块链

    #!/usr/bin/env python # encoding: utf-8 ''' 我们要创建一个 Blockchain 类 ,他的构造函数创建了一个初始化的空列表(要存储我们的区块链),并且另一 ...

  2. 40多行python代码开发一个区块链。

    40多行python代码开发一个区块链?可信吗?我们将通过Python 2动手开发实现一个迷你区块链来帮你真正理解区块链技术的核心原理.python开发区块链的源代码保存在Github. 尽管有人认为 ...

  3. cpp 区块链模拟示例(一)工程建立

    /* 作 者: itdef 欢迎转帖 请保持文本完整并注明出处 技术博客 http://www.cnblogs.com/itdef/ 技术交流群 群号码:432336863欢迎c c++ window ...

  4. AbelSu的区块链笔记

    最近几年,像比特币.以太坊.ICO.区块链等概念突然成为互联网热门话题,今天写这篇博客,也是做一些笔记,大概说一下对这个的解释和其他相关内容. 区块链: 区块链是分布式数据存储.点对点传输.共识机制. ...

  5. 50行Python代码构建小型区块链

    本文介绍了如何使用python构建一个小型的区块链技术,使用Python2实现,代码不到50行. Although some think blockchain is a solution waitin ...

  6. 用不到 50 行的 Python 代码构建最小的区块链

    引用 译者注:随着比特币的不断发展,它的底层技术区块链也逐步走进公众视野,引起大众注意.本文用不到50行的Python代码构建最小的数据区块链,简单介绍了区块链去中心化的结构与其实现原理. 尽管一些人 ...

  7. 用 Python 撸一个区块链

    本文翻译自 Daniel van Flymen 的文章 Learn Blockchains by Building One 略有删改.原文地址:https://hackernoon.com/learn ...

  8. 用Python从零开始创建区块链

    本文主要内容翻译自Learn Blockchains by Building One 本文原始链接,转载请注明出处. 作者认为最快的学习区块链的方式是自己创建一个,本文就跟随作者用Python来创建一 ...

  9. 51行代码实现简单的PHP区块链

    本文原始地址:php区块链demo 今年区块链特别火,我也很火啊.我火什么呢.前几年,公众平台出现,还得花时间去学去看,后来小程序出现,又得花时间精力去学去看.现在比特币.以太坊等去中心化货币带起了区 ...

随机推荐

  1. Python之数据结构:序列

    一.序列 1.列表.元组和字符串都是序列 二.序列的两个特点:索引操作符和切片操作符 1.索引操作符:从序列中抓取一个特定项目 下标操作:使用索引获取序列中的单个项目: eg:shoplist[0] ...

  2. 《插件》一个比较好用的 chrome浏览器的json格式化插件

    插件名: JSON-Handle   下载地址:         http://jsonhandle.sinaapp.com/ 插件下载后,在浏览器输入:chrome://extensions/ 将下 ...

  3. :nth-child :nth-type-of用法详解

     ele:nth-of-type(n) 是指父元素下ele元素里的第n个ele:nth-child(n) 是指父元素下第n个元素且这个元素为ele 

  4. 2017-2018-2 20179204《网络攻防实践》第十三周学习总结 python实现国密算法

    国密商用算法是指国密SM系列算法,包括基于椭圆曲线的非对称公钥密码SM2算法.密码杂凑SM3算法.分组密码SM4算法,还有只以IP核形式提供的非公开算法流程的对称密码SM1算法等. 第1节 SM2非对 ...

  5. 创建外网 ext

    虽然外部网络是已经存在的网络,但我们还是需要在 Neutron 中定义外部网络的对象,这样 router 才知道如何将租户网络和外部网络连接起来. 上一节我们已经为创建外部网络配置了ML2,本节将通过 ...

  6. k-mean聚类学习笔记

    才发现k-means 聚类这么简单,-_-|| 首先讲一下最朴素的k-means, 首先k-means 是一个迭代过程. 所以我们需要先确定初始,最简单的一个办法就是随机从样本中抽取k个出来,作为初始 ...

  7. [LeetCode] Add Two Numbers 链表

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. poj 2528 Mayor's posters 线段树 || 并查集 离线处理

    题目链接 题意 用不同颜色的线段覆盖数轴,问最终数轴上有多少种颜色? 注:只有最上面的线段能够被看到:即,如果有一条线段被其他的线段给完全覆盖住,则这个颜色是看不到的. 法一:线段树 按题意按顺序模拟 ...

  9. 插入排序Insertion sort(转)

    插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕.     插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序,折半插入排序留到“查找”内 ...

  10. IPC最快的方式----共享内存(shared memory)

    在linux进程间通信的方式中,共享内存是一种最快的IPC方式.因此,共享内存用于实现进程间大量的数据传输,共享内存的话,会在内存中单独开辟一段内存空间,这段内存空间有自己特有的数据结构,包括访问权限 ...