热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

hash签名java_java开发区块链只需150行代码

原标题:java开发区块链只需150行代码本文帮助你理解什么是区块链。将通过java开发区块链的实战学习方式,用Java创建开发一个基本的区块链

原标题:java开发区块链只需150行代码

本文帮助你理解什么是区块链。将通过java开发区块链的实战学习方式,用 Java创建开发一个基本的区块链,实现简单的工作量证明系统。Java开发区块链的源代码保存在Github。

需要指出的是,我们用150行代码构建的区块链达不到生产级别的,它只是为了 帮助你更好的理解区块链的概念。

创建区块链

区块链就是一串或者是一系列区块的集合,类似于链表的概念,每个区块都指向于后面一个区块,然后顺序的连接在一起。那么每个区块中的内容是什么呢?在区块链中的每一个区块都存放了很多 很有价值的信息,主要包括三个部分:自己的数字签名,上一个区块的数字签名,还有一切需要加密的数据(这些数据在比特币中就相当于是交易的信息,它是加密货币的本质)。每个数字签名 不但证明了自己是特有的一个区块,而且指向了前一个区块的来源,让所有的区块在链条中可以串起来,而数据就是一些特定的信息,你可以按照业务逻辑来保存业务数据。

这里的hash指的就是数字签名

所以每一个区块不仅包含前一个区块的hash值,同时包含自身的一个hash值,自身的hash值是通过 之前的hash值和数据data通过hash计算出来的。如果前一个区块的数据一旦被篡改了,那么前一个 区块的hash值也会同样发生变化(因为数据也被计算在内),这样也就导致了所有后续的区块中的 hash值。所以计算和比对hash值会让我们检查到当前的区块链是否是有效的,也就避免了数据被恶意篡改的可能性,因为篡改数据就会改变hash值并破坏整个区块链。

定义区块链的类Block:

import java.util.Date;

public class Block {

public String hash;

public String previousHash;

private String data; //our data will be a simple message.

private long timeStamp; //as number of milliseconds since 1/1/1970.

//Block Constructor.

public Block(String data,String previousHash ) {

this.data = data;

this.previousHash = previousHash;

this.timeStamp = new Date().getTime();

}

}

正如你可以看到我们的基本块包含String hash,它将保存我们的数字签名。变量 previoushash保存前一个块的hash和String data来保存我们的块数据

创建数字签名

熟悉加密算法的朋友们,Java方式可以实现的加密方式有很多,例如BASE、MD、RSA、SHA等等,我在这里选用了SHA256这种加密方式,SHA(Secure Hash Algorithm)安全散列算法,这种算法 的特点是数据的少量更改会在Hash值中产生不可预知的大量更改,hash值用作表示大量数据的 固定大小的唯一值,而SHA256算法的hash值大小为256位。之所以选用SHA256是因为它的大小正合适, 一方面产生重复hash值的可能性很小,另一方面在区块链实际应用过程中,有可能会产生大量的区块,而使得信息量很大,那么256位的大小就比较恰当了。

下面我创建了一个StringUtil方法来方便调用SHA256算法

import java.security.MessageDigest;

public class StringUtil {

//Applies Sha256 to a string and returns the result.

public static String applySha256(String input){

try {

MessageDigest digest = MessageDigest.getInstance(SHA-256);

//Applies sha256 to our input,

byte[] hash = digest.digest(input.getBytes(UTF-8));

StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal

for (int i = 0; i hash.length; i++) {

String hex = Integer.toHexString(0xff hash[i]);

if(hex.length() == 1) hexString.append('0');

hexString.append(hex);

}

return hexString.toString();

}

catch(Exception e) {

throw new RuntimeException(e);

}

}

}

或许你不完全理解上述代码的含义,但是你只要理解所有的输入调用此方法后均会生成一个独一无二的hash值(数字签名), 而这个hash值在区块链中是非常重要的。

接下来让我们在Block类中应用 方法 applySha256 方法,其主要的目的就是计算hash值,我们计算的hash值 应该包括区块中所有我们不希望被恶意篡改的数据,在我们上面所列的Block类中就一定包括previousHash,data和timeStamp,

public String calculateHash() {

String calculatedhash = StringUtil.applySha256(

previousHash +

Long.toString(timeStamp) +

data

);

return calculatedhash;

}

然后把这个方法加入到Block的构造函数中去

public Block(String data,String previousHash ) {

this.data = data;

this.previousHash = previousHash;

this.timeStamp = new Date().getTime();

this.hash = calculateHash(); //Making sure we do this after we set the other values.

}

测试

在主方法中让我们创建一些区块,并把其hash值打印出来,来看看是否一切都在我们的掌控中。

第一个块称为创世块,因为它是头区块,所以我们只需输入“0”作为前一个块的previous hash。

public class NoobChain {

public static void main(String[] args) {

Block genesisBlock = new Block(Hi im the first block, 0);

System.out.println(Hash for block 1 : + genesisBlock.hash);

Block secondBlock = new Block(Yo im the second block,genesisBlock.hash);

System.out.println(Hash for block 2 : + secondBlock.hash);

Block thirdBlock = new Block(Hey im the third block,secondBlock.hash);

System.out.println(Hash for block 3 : + thirdBlock.hash);

}

}

打印输出结果:

Hash for block 1: f6d1bc5f7b0016eab53ec022db9a5d9e1873ee78513b1c666696e66777fe55fb

Hash for block 2: 6936612b3380660840f22ee6cb8b72ffc01dbca5369f305b92018321d883f4a3

Hash for block 3: f3e58f74b5adbd59a7a1fc68c97055d42e94d33f6c322d87b29ab20d3c959b8f

每一个区块都必须要有自己的数据签名即hash值,这个hash值依赖于自身的信息(data)和上 一个区块的数字签名(previousHash),但这个还不是区块链,下面让我们存储区块到数组中,这里我会引入gson包,目的是可以用json方式查看整个一条区块链结构。

import java.util.ArrayList;

import com.google.gson.GsonBuilder;

public class NoobChain {

public static ArrayListblockchain = new ArrayList();

public static void main(String[] args) {

//add our blocks to the blockchain ArrayList:

blockchain.add(new Block(Hi im the first block, 0));

blockchain.add(new Block(Yo im the second block,blockchain.get(blockchain.size()-1).hash));

blockchain.add(new Block(Hey im the third block,blockchain.get(blockchain.size()-1).hash));

String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);

System.out.println(blockchainJson);

}

}

这样的输出结构就更类似于我们所期待的区块链的样子。

检查区块链的完整性

在主方法中增加一个isChainValid()方法,目的是循环区块链中的所有区块并且比较hash值,这个方法用来检查hash值是否是于计算出来的hash值相等,同时previousHash值是否和前一个 区块的hash值相等。或许你会产生如下的疑问,我们就在一个主函数中创建区块链中的区块,所以不存在被修改的可能性,但是你要注意的是,区块链中的一个核心概念就是去中心化, 每一个区块可能是在网络中的某一个节点中产生的,所以很有可能某个节点把自己节点中的数据修改了,那么根据上述的理论数据改变会导致整个区块链的破裂,也就是区块链就无效了。

public static Boolean isChainValid() {

Block currentBlock;

Block previousBlock;

//loop through blockchain to check hashes:

for(int i=1; i blockchain.size(); i++) {

currentBlock = blockchain.get(i);

previousBlock = blockchain.get(i-1);

//compare registered hash and calculated hash:

if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){

System.out.println(Current Hashes not equal);

return false;

}

//compare previous hash and registered previous hash

if(!previousBlock.hash.equals(currentBlock.previousHash) ) {

System.out.println(Previous Hashes not equal);

return false;

}

}

return true;

}

任何区块链中区块的一丝一毫改变都会导致这个函数返回false,也就证明了区块链无效了。

在比特币网络中所有的网络节点都分享了它们各自的区块链,然而最长的有效区块链是被全网所统一承认的,如果有人恶意来篡改之前的数据,然后创建一条更长的区块链并全网 发布呈现在网络中,我们该怎么办呢?这就涉及到了区块链中另外一个重要的概念工作量证明,这里就不得不提及一下hashcash,这个概念最早来自于Adam Back的一篇论文,主要应用于邮件过滤和比特币中防止双重支付。

挖矿

这里我们要求挖矿者做工作量证明,具体的方式是在区块中尝试不同的参数值直到它的hash 值是从一系列的0开始的。让我们添加一个名为nonce的int类型以包含在我们的calculatehash()方法中,以及需要的mineblock()方法

import java.util.Date;

public class Block {

public String hash;

public String previousHash;

private String data; //our data will be a simple message.

private long timeStamp; //as number of milliseconds since 1/1/1970.

private int nonce;

//Block Constructor.

public Block(String data,String previousHash ) {

this.data = data;

this.previousHash = previousHash;

this.timeStamp = new Date().getTime();

this.hash = calculateHash(); //Making sure we do this after we set the other values.

}

//Calculate new hash based on blocks contents

public String calculateHash() {

String calculatedhash = StringUtil.applySha256(

previousHash +

Long.toString(timeStamp) +

Integer.toString(nonce) +

data

);

return calculatedhash;

}

public void mineBlock(int difficulty) {

String target = new String(new char[difficulty]).replace('\0', '0'); //Create a string with difficulty * 0

while(!hash.substring( 0, difficulty).equals(target)) {

nonce ++;

hash = calculateHash();

}

System.out.println(Block Mined!!! : + hash);

}

}

mineBlock()方法中引入了一个int值称为difficulty难度,低的难度比如1和2,普通的电脑基本都可以马上计算出来, 我的建议是在4-6之间进行测试,普通电脑大概会花费3秒时间,在莱特币中难度大概围绕在442592左右,而在 比特币中每一次挖矿都要求大概在10分钟左右,当然根据所有网络中的计算能力,难度也会不断的进行修改。

我们在NoobChain类 中增加difficulty这个静态变量。

1

public static int difficulty = 5;

这样我们必须修改主方法中让创建每个新区块时必须触发mineBlock()方法,而isChainValid()方法用来检查每 个区块的hash值是否正确,整个区块链是否是有效的。

import java.util.ArrayList;

import com.google.gson.GsonBuilder;

public class NoobChain {

public static ArrayListblockchain = new ArrayList();

public static int difficulty = 5;

public static void main(String[] args) {

//add our blocks to the blockchain ArrayList:

blockchain.add(new Block(Hi im the first block, 0));

System.out.println(Trying to Mine block 1... );

blockchain.get(0).mineBlock(difficulty);

blockchain.add(new Block(Yo im the second block,blockchain.get(blockchain.size()-1).hash));

System.out.println(Trying to Mine block 2... );

blockchain.get(1).mineBlock(difficulty);

blockchain.add(new Block(Hey im the third block,blockchain.get(blockchain.size()-1).hash));

System.out.println(Trying to Mine block 3... );

blockchain.get(2).mineBlock(difficulty);

System.out.println(\nBlockchain is Valid: + isChainValid());

String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);

System.out.println(\nThe block chain: );

System.out.println(blockchainJson);

}

public static Boolean isChainValid() {

Block currentBlock;

Block previousBlock;

String hashTarget = new String(new char[difficulty]).replace('\0', '0');

//loop through blockchain to check hashes:

for(int i=1; i blockchain.size(); i++) {

currentBlock = blockchain.get(i);

previousBlock = blockchain.get(i-1);

//compare registered hash and calculated hash:

if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){

System.out.println(Current Hashes not equal);

return false;

}

//compare previous hash and registered previous hash

if(!previousBlock.hash.equals(currentBlock.previousHash) ) {

System.out.println(Previous Hashes not equal);

return false;

}

//check if hash is solved

if(!currentBlock.hash.substring( 0, difficulty).equals(hashTarget)) {

System.out.println(This block hasn't been mined);

return false;

}

}

return true;

}

}

打印:

Connected to the target VM, address: '127.0.0.1:61863', transport: 'socket'

Trying to Mine block 1...

Block Mined!!! : 0000016667d4240e9c30f53015310b0ec6ce99032d7e1d66d670afc509cab082

Trying to Mine block 2...

Block Mined!!! : 000002ea55735bea4cac7e358c7b0d8d81e8ca24021f5f85211bf54fd4ac795a

Trying to Mine block 3...

Block Mined!!! : 000000576987e5e9afbdf19b512b2b7d0c56db0e6ca49b3a7e638177f617994b

Blockchain is Valid: true

[

{

hash: 0000016667d4240e9c30f53015310b0ec6ce99032d7e1d66d670afc509cab082,

previousHash: 0,

data: first,

timeStamp: 1520659506042,

nonce: 618139

},

{

hash: 000002ea55735bea4cac7e358c7b0d8d81e8ca24021f5f85211bf54fd4ac795a,

previousHash: 0000016667d4240e9c30f53015310b0ec6ce99032d7e1d66d670afc509cab082,

data: second,

timeStamp: 1520659508825,

nonce: 1819877

},

{

hash: 000000576987e5e9afbdf19b512b2b7d0c56db0e6ca49b3a7e638177f617994b,

previousHash: 000002ea55735bea4cac7e358c7b0d8d81e8ca24021f5f85211bf54fd4ac795a,

data: third,

timeStamp: 1520659515910,

nonce: 1404341

}

]

经过测试增加一个新的区块即挖矿必须花费一定时间,大概是3秒左右,你可以提高difficulty难度来看, 它是如何影响数据难题所花费的时间的

如果有人在你的区块链系统中恶意篡改数据:

· 他们的区块链是无效的。

· 他们无法创建更长的区块链

· 网络中诚实的区块链会在长链中更有时间的优势

因为篡改的区块链将无法赶上长链和有效链,除非他们比你网络中所有的节点拥有更大的计算速度,可能是未来的量子计算机或者是其他什么。

你已经完成了你的基本区块链!返回搜狐,查看更多

责任编辑:



推荐阅读
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 加密、解密、揭秘
    谈PHP中信息加密技术同样是一道面试答错的问题,面试官问我非对称加密算法中有哪些经典的算法?当时我愣了一下,因为我把非对称加密与单项散列加 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • MybatisPlus入门系列(13) MybatisPlus之自定义ID生成器
    数据库ID生成策略在数据库表设计时,主键ID是必不可少的字段,如何优雅的设计数据库ID,适应当前业务场景,需要根据需求选取 ... [详细]
  • 前面刚有AWS开战MongoDB,双方“隔空互呛”,这厢又曝出2亿+简历信息泄露——MongoDB的这场开年似乎“充实”得过分了些。长期以来,作为“最受欢迎的NoSQL数据库”,M ... [详细]
  • 【指南】我们可以从去中心化的社区建设中学到什么?
    本文属于老雅痞原创文章,转载规矩不变,给我们打声招呼~转载请微信联系:huangdiezi,更多 ... [详细]
  • 行情概览51BB8财经对接全网大数据行情,收录币种4,416个,截至12月3日17时00分,24h交易量130.3亿美元, ... [详细]
  • 加密派对结束 仍有人坚守行业
    本文属于老雅痞原创文章,转 ... [详细]
  • 本文分享了一位Android开发者多年来对于Android开发所需掌握的技能的笔记,包括架构师基础、高级UI开源框架、Android Framework开发、性能优化、音视频精编源码解析、Flutter学习进阶、微信小程序开发以及百大框架源码解读等方面的知识。文章强调了技术栈和布局的重要性,鼓励开发者做好学习规划和技术布局,以提升自己的竞争力和市场价值。 ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
  • 谁是更好的选择?
    原文作者:DefiSurfer编译:LlamaC ... [详细]
  • 为元宇宙提供动力的 5 项重要技术
    元宇宙是你肯定听说过的东西。在过去的一年里,每个人都在谈论它。这是技术领域的下一件大事。Bloomberg情报高级行业分析师马修·坎特曼(MatthewKanterman)的分析显 ... [详细]
  • iMesh网站数据在暗网上被出售
    iMesh公司曾是美国三大音乐视频分享服务提供商之一,但是据国外媒体报道,这家公司近期正式对外宣布破产。iMesh是一个文件分享软件,它能够让 ... [详细]
  • 《头号玩家》:区块链+VR构成的明天!
        强烈安利各位朋友去电影院看《头号玩家》,加好友,我自掏腰包送你两张电影票!一、现实世界≠虚拟世界——被黑的哥伦布:   现实中美好的城市可能在虚拟世界变成罪恶的哥伦布贫民窟 ... [详细]
author-avatar
对不起太依赖你
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有