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

加密算法是python自带的

需要导入hashlib

import hashlib as hash

sha = hasher.sha256()

sha.update('your content')

print sha.hexdigest()

输出:baca6a6db216faf43b107e5f00e20eaf22edc75e922de5ccc08c16b91b9eb3bd

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

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

下面贴出源代码

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3.  
  4. import hashlib as hasher
  5. import datetime as date
  6.  
  7. class Block:
  8. def __init__(self, index, timestamp, data, previous_hash):
  9. self.index = index
  10. self.timestamp = timestamp
  11. self.data = data
  12. self.previous_hash = previous_hash
  13. self.hash = self.hash_block()
  14.  
  15. def hash_block(self):
  16. sha = hasher.sha256()
  17. sha.update(str(self.index) +
  18. str(self.timestamp) +
  19. str(self.data) +
  20. str(self.previous_hash))
  21. return sha.hexdigest()
  22.  
  23. def create_genesis_block():
  24. return Block(0, date.datetime.now(), "Block Data", "")
  25.  
  26. def next_block(last_block):
  27. this_index = last_block.index + 1
  28. this_timestamp = date.datetime.now()
  29. this_data = "Hey! I'm block " + str(this_index)
  30. this_hash = last_block.hash
  31. return Block(this_index, this_timestamp, this_data, this_hash)
  32.  
  33. blockchain = [create_genesis_block()]
  34. previous_block = blockchain[0]
  35.  
  36. #生成20个为例
  37. num_of_blocks_to_add = 20
  38.  
  39. for i in range(0, num_of_blocks_to_add):
  40. block_to_add = next_block(previous_block)
  41. blockchain.append(block_to_add)
  42. previous_block = block_to_add
  43. # Tell everyone about it!
  44. print "Block #{} has been added to the blockchain!".format(block_to_add.index)
  45. 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. Java EE 学习(2):使用 IDEA 开发 最简java web

    参考:http://www.cnblogs.com/carsonzhu/p/5468223.html 使用IntelliJ IDEA开发java web 前言:由于本人以前简略的学习过 java 语言 ...

  2. POJ3311 Hie with the Pie

    The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortu ...

  3. 【转】Resharper上手指南

    原文发布时间为:2011-02-16 -- 来源于本人的百度文章 [由搬家工具导入] 我是visual studio的忠实用户,从visual studio 6一直用到了visual studio 2 ...

  4. FCKEDITOR配置说明

    原文发布时间为:2009-10-12 -- 来源于本人的百度文章 [由搬家工具导入] fckeditor config.js配置2009-02-13 14:36 FCKConfig.CustomCon ...

  5. [LeetCode] 4Sum hash表

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  6. linux内核情景分析之强制性调度

    从系统调用返回到用户空间是否调度,从ret_with_reschedule可看出,是否真正调度,取决于当前进程的pcb中的need_resched是否设置为1,那如何设置为1取决于以下几种情况: 时间 ...

  7. mac 重置mysql root密码

    1. 关闭mysql服务 sudo /usr/local/mysql/support-files/mysql.server stop 如果出现Starting mysqld daemon with d ...

  8. SQL语句的执行顺序(转载+不同意见)

    MySQL的语句一共分为11步,如下图所标注的那样,最先执行的总是FROM操作,最后执行的是LIMIT操作.其中每一个操作都会产生一张虚拟的表,这个虚拟的表作为一个处理的输入,只是这些虚拟的表对用户来 ...

  9. C#将图片进行马赛克处理

    /// <summary> /// 马赛克处理 /// </summary> /// <param name="bitmap"></par ...

  10. python3模拟扑克牌

    python3.6环境 import collections from random import choice Card=collections.namedtuple('Card',['rank', ...