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 今年区块链特别火,我也很火啊.我火什么呢.前几年,公众平台出现,还得花时间去学去看,后来小程序出现,又得花时间精力去学去看.现在比特币.以太坊等去中心化货币带起了区 ...
随机推荐
- BZOJ4516 [Sdoi2016]生成魔咒 【后缀自动机】
题目 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔咒串 [1,2]. 一个魔咒串 S 的非空字串被称为魔咒串 S 的生成魔咒. 例如 S=[1,2, ...
- Linux系统——机制策略(一)
机制策略(一) 形而上谓之道:形而下谓之器: ————易经 LinuxUnix设计理念提供的一种机制不是策略:1.如果说机制是一种框架,那么,策略就是填充框架的一个个具体实施.机制提供的就是一种开放而 ...
- Codeforces Round #363 (Div. 2) A 水
Description There will be a launch of a new, powerful and unusual collider very soon, which located ...
- FZU 2168 前缀和+dp递推
Description 部队中共有N个士兵,每个士兵有各自的能力指数Xi,在一次演练中,指挥部确定了M个需要防守的地点,按重要程度从低到高排序,依次以数字1到M标注每个地点的重要程度,指挥部将选择 ...
- gradle ofbiz 16 开发环境搭建
原 gradle ofbiz 16 开发环境搭建 2017年02月13日 10:59:19 阅读数:2702 1.安装jdk 2.配置jdk环境变量 3.eclipse 安装svn 插件 4.svn下 ...
- 江南乐(bzoj 3576)
Description 小A是一个名副其实的狂热的回合制游戏玩家.在获得了许多回合制游戏的世界级奖项之后,小A有一天突然想起了他小时候在江南玩过的一个回合制游戏. 游戏的规则是这样的,首先给定一 ...
- poj 1795 DNA Laboratory
DNA Laboratory Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 2892 Accepted: 516 Des ...
- 回发或回调参数无效 “HtmlSelect”不能有类型为“LiteralControl”的子级
原文发布时间为:2009-11-14 -- 来源于本人的百度文章 [由搬家工具导入] 回发或回调参数无效 “HtmlSelect”不能有类型为“LiteralControl”的子级 出现这两个错误,一 ...
- AVRStudio 6 设置F_CPU时钟频率
具体如下: 1>右键项目属性 2>根据语言选择一下,C或C++
- [LeetCode] Remove Duplicates from Sorted List II 链表
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...