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", "") 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实现区块链代码的更多相关文章
- python搭建区块链
#!/usr/bin/env python # encoding: utf-8 ''' 我们要创建一个 Blockchain 类 ,他的构造函数创建了一个初始化的空列表(要存储我们的区块链),并且另一 ...
- 40多行python代码开发一个区块链。
40多行python代码开发一个区块链?可信吗?我们将通过Python 2动手开发实现一个迷你区块链来帮你真正理解区块链技术的核心原理.python开发区块链的源代码保存在Github. 尽管有人认为 ...
- cpp 区块链模拟示例(一)工程建立
/* 作 者: itdef 欢迎转帖 请保持文本完整并注明出处 技术博客 http://www.cnblogs.com/itdef/ 技术交流群 群号码:432336863欢迎c c++ window ...
- AbelSu的区块链笔记
最近几年,像比特币.以太坊.ICO.区块链等概念突然成为互联网热门话题,今天写这篇博客,也是做一些笔记,大概说一下对这个的解释和其他相关内容. 区块链: 区块链是分布式数据存储.点对点传输.共识机制. ...
- 50行Python代码构建小型区块链
本文介绍了如何使用python构建一个小型的区块链技术,使用Python2实现,代码不到50行. Although some think blockchain is a solution waitin ...
- 用不到 50 行的 Python 代码构建最小的区块链
引用 译者注:随着比特币的不断发展,它的底层技术区块链也逐步走进公众视野,引起大众注意.本文用不到50行的Python代码构建最小的数据区块链,简单介绍了区块链去中心化的结构与其实现原理. 尽管一些人 ...
- 用 Python 撸一个区块链
本文翻译自 Daniel van Flymen 的文章 Learn Blockchains by Building One 略有删改.原文地址:https://hackernoon.com/learn ...
- 用Python从零开始创建区块链
本文主要内容翻译自Learn Blockchains by Building One 本文原始链接,转载请注明出处. 作者认为最快的学习区块链的方式是自己创建一个,本文就跟随作者用Python来创建一 ...
- 51行代码实现简单的PHP区块链
本文原始地址:php区块链demo 今年区块链特别火,我也很火啊.我火什么呢.前几年,公众平台出现,还得花时间去学去看,后来小程序出现,又得花时间精力去学去看.现在比特币.以太坊等去中心化货币带起了区 ...
随机推荐
- Java面试题之在多线程情况下,单例模式中懒汉和饿汉会有什么问题呢?
懒汉模式和饿汉模式: public class Demo { //private static Single single = new Single();//饿汉模式 private static S ...
- hdu 2189 dp
/* 类似完全背包,容量为n的背包用素数填,求满背包的种数 dp(i,j)表示用不超过i的素数组成的j的种数 dp[i][j]=dp[i-1][j],若i为素数则dp[i][j]+=dp[i][j-i ...
- [论文]A Link-Based Cluster Ensemble Approach for Categorical Data Clustering
http://www.cnblogs.com/Azhu/p/4137131.html 这篇论文建议先看了上面这一遍,两篇作者是一样的,方法也一样,这一片论文与上面的不同点在于,使用的数据集是目录数据, ...
- 7天学习opengl入门
http://blog.csdn.net/slience_perseverance/article/details/8096233 10月13号下午3:00队长给我开了一个会,10.14号开始学习op ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---16
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- Fio IO性能测试
fio-2.1.2-1.el5.rf.x86_64 介绍 fio different types of I/O engines (sync, mmap, libaio, posixaio, SG v3 ...
- AC日记——文艺平衡树 洛谷 P3391
文艺平衡树 思路: splay翻转操作模板: 虚拟最左最右端点,然后每次都把l翻转到root,r+2翻转到root的右节点: 然后在r+2的左节点上打标记: 标记需要在旋转,rank,print时下放 ...
- SVG描边动画实现过程
准备工具:Adobe AI+PS 1.确定SVG画布的大小,在PS中切出需要描边效果的区域,以此区域的大小做为SVG容器的大小. 2.将PS中切好的图片直接拖拽到AI中 3.使用AI中的钢 ...
- JMeter进行http接口测试
Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对Http接口的测试. 一.开发接口测试案例的整体方案: 第一步:我们要 ...
- 洛谷——P1306 斐波那契公约数
P1306 斐波那契公约数 题目描述 对于Fibonacci数列:1,1,2,3,5,8,13......大家应该很熟悉吧~~~但是现在有一个很“简单”问题:第n项和第m项的最大公约数是多少? 输入输 ...