当前位置: 首页 > news >正文

简简单单区块链

   difficulty可以改成8

package org.example;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;class Block {private int index;private long timestamp;private String data;private String previousHash;private String hash;private int nonce;public Block(int index, long timestamp, String data, String previousHash) {this.index = index;this.timestamp = timestamp;this.data = data;this.previousHash = previousHash;this.nonce = 0;this.hash = calculateHash();}public String calculateHash() {try {String input = index + timestamp + data + previousHash + nonce;MessageDigest digest = MessageDigest.getInstance("SHA-256");byte[] hashBytes = digest.digest(input.getBytes());// 将字节数组转换为十六进制字符串StringBuilder hexString = new StringBuilder();for (byte b : hashBytes) {String hex = Integer.toHexString(0xff & b);if (hex.length() == 1) hexString.append('0');hexString.append(hex);}return hexString.toString();} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);}}public void mineBlock(int difficulty) {String target = new String(new char[difficulty]).replace('\0', '0');while (!hash.substring(0, difficulty).equals(target)) {nonce++;hash = calculateHash();}System.out.println("Block mined! Nonce: " + nonce + ", Hash: " + hash);}// Getterspublic int getIndex() { return index; }public long getTimestamp() { return timestamp; }public String getData() { return data; }public String getPreviousHash() { return previousHash; }public String getHash() { return hash; }public int getNonce() { return nonce; }@Overridepublic String toString() {return "Block #" + index + " [Hash: " + hash + ", Prev: " + previousHash + ", Nonce: " + nonce + ", Data: " + data + "]";}public void setPreviousHash(String hash) {this.previousHash=hash;}
}class Blockchain {private List<Block> chain;private int difficulty;public Blockchain(int difficulty) {this.chain = new ArrayList<>();this.difficulty = difficulty;createGenesisBlock();}private void createGenesisBlock() {Block genesisBlock = new Block(0, System.currentTimeMillis(), "Genesis Block", "0");chain.add(genesisBlock);System.out.println("Genesis Block created!");}public Block getLatestBlock() {return chain.get(chain.size() - 1);}public void addBlock(Block newBlock) {newBlock.setPreviousHash(getLatestBlock().getHash());newBlock.mineBlock(difficulty);chain.add(newBlock);System.out.println("Block #" + newBlock.getIndex() + " added to the blockchain!");}public boolean isChainValid() {for (int i = 1; i < chain.size(); i++) {Block currentBlock = chain.get(i);Block previousBlock = chain.get(i - 1);// 验证当前区块的哈希是否正确if (!currentBlock.getHash().equals(currentBlock.calculateHash())) {System.out.println("Block #" + currentBlock.getIndex() + " has invalid hash!");return false;}// 验证与前一个区块的链接if (!currentBlock.getPreviousHash().equals(previousBlock.getHash())) {System.out.println("Block #" + currentBlock.getIndex() + " has invalid previous hash!");return false;}// 验证工作量证明(难度要求)String target = new String(new char[difficulty]).replace('\0', '0');if (!currentBlock.getHash().substring(0, difficulty).equals(target)) {System.out.println("Block #" + currentBlock.getIndex() + " does not meet difficulty requirement!");return false;}}return true;}public void printChain() {System.out.println("\n===== Blockchain =====");for (Block block : chain) {System.out.println(block);}System.out.println("======================");}// 内部类扩展Block以添加setPreviousHash方法static class BlockExtended extends Block {public BlockExtended(int index, long timestamp, String data, String previousHash) {super(index, timestamp, data, previousHash);}public void setPreviousHash(String previousHash) {// 使用反射或其他方法设置,这里简化处理// 实际应用中可能需要修改Block类设计// 这里仅用于演示目的try {java.lang.reflect.Field field = Block.class.getDeclaredField("previousHash");field.setAccessible(true);field.set(this, previousHash);} catch (Exception e) {e.printStackTrace();}}}
}public class SimpleBlockchain {public static void main(String[] args) {// 创建难度为2的区块链Blockchain blockchain = new Blockchain(2);// 添加新区块Blockchain.BlockExtended block1 = new Blockchain.BlockExtended(1, System.currentTimeMillis(), "Transaction Data 1", "");blockchain.addBlock(block1);Blockchain.BlockExtended block2 = new Blockchain.BlockExtended(2, System.currentTimeMillis(), "Transaction Data 2", "");blockchain.addBlock(block2);// 打印区块链blockchain.printChain();// 验证区块链System.out.println("\nIs blockchain valid? " + blockchain.isChainValid());// 尝试篡改数据System.out.println("\nAttempting to tamper with Block 1 data...");// 注意:实际应用中需要更安全的数据访问方式// 这里仅用于演示篡改检测Block blockToTamper = blockchain.getLatestBlock(); // 获取最后一个区块try {java.lang.reflect.Field dataField = Block.class.getDeclaredField("data");dataField.setAccessible(true);dataField.set(blockToTamper, "Tampered Data");} catch (Exception e) {e.printStackTrace();}// 再次验证区块链System.out.println("Is blockchain valid after tampering? " + blockchain.isChainValid());}
}
  1. ​Block 类​​:

    • 表示区块链中的单个区块

    • 包含索引、时间戳、数据、前一个区块的哈希、当前哈希和随机数(nonce)

    • calculateHash()方法使用 SHA-256 算法计算区块哈希

    • mineBlock()方法实现工作量证明(挖矿)过程

  2. ​Blockchain 类​​:

    • 管理整个区块链

    • createGenesisBlock()创建创世区块(第一个区块)

    • addBlock()添加新区块并执行挖矿

    • isChainValid()验证区块链的完整性

    • printChain()打印整个区块链

  3. ​BlockExtended 内部类​​:

    • 扩展 Block 类以添加设置前一个哈希的方法

    • 实际应用中可能需要修改 Block 类设计

  4. ​SimpleBlockchain 主类​​:

    • 演示区块链的创建、添加区块和验证过程

    • 包含篡改检测演示

程序功能:

  1. 创建创世区块

  2. 添加新区块并执行工作量证明(挖矿)

  3. 打印整个区块链

  4. 验证区块链完整性

  5. 演示篡改检测

http://www.dtcms.com/a/475151.html

相关文章:

  • 泗洪县城乡建设局网站网站策划包括哪些内容
  • 河北建设执业资格注册中心网站注册公司流程和费用2020
  • 德阳有哪些做网站的公司网站空间配置
  • Java、C语言、Python、PHP、C#、C++六种编程语言的核心特性与实际应用场景对比分析
  • 如何做计算机网站个人备案网站能用公司
  • 外包做网站的要求怎么写做公司网站需要多久
  • LeetCode 分类刷题:25. K 个一组翻转链表
  • Redis-Set
  • 上海招聘用的最多的网站网站过程中遇到问题
  • 服务器打不开网站票务网站做酒店推荐的目的
  • 【完整源码+数据集+部署教程】 淡水鱼种类识别图像分割系统源码和数据集:改进yolo11-SDI
  • 西安网站建设中心常用的网页设计软件
  • Java 大视界 -- Java 大数据在智慧农业精准灌溉与施肥决策中的应用
  • 做竞价的网站网站结构seo
  • 【完整源码+数据集+部署教程】 手部清洗步骤识别系统源码和数据集:改进yolo11-DRBNCSPELAN
  • 亚马逊网站建设进度计划表江门网站设计素材
  • 网站建设详情页做旅游网站用什么颜色
  • 学习Java第三十八天——黑马点评69~74
  • java实现ofd转pdf
  • php网站开发实战教程国外免费搭建网站
  • 崇明区建设镇网站越秀移动网站建设
  • 【Android】MVP架构模式
  • WAV文件结构和PCM数据转存WAV文件
  • 美妆网站开发规划书有没有帮忙做问卷调查的网站
  • 绵阳手机网站建设分公司注册流程及需要的材料
  • 手工活接单在家做有正规网站吗湖南省郴州市天气预报
  • Linux中重定向举例!!
  • 建设厅八大员在哪个网站查询视频拍摄器材
  • 济南网站建设推广服务网站建设的目的及目标
  • php 网站开发缓存有那几种关键词挖掘ppt