即使数据集更改为另一个值,哈希也不会改变
Hash does not change even when data set is changed to another value
我用 java 创建了简单的区块链程序。目前我正在为交易数据集生成哈希。当我更改交易数据集时,哈希值没有改变
首先,我创建了一个交易 class,其中包含交易名称、交易 ID 和交易时间戳值。目前 transactionDate 和 transactionID 是自动生成的。
这是包裹MainNode.pkg;
import NodeData.pkg.TransactionClass;
import sun.util.logging.PlatformLogger;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author melanrashitha
* @date 11/12/19
* @project BlockChainArchController
*/
public class MainNode {
private int hash;
private int prevHash;
private TransactionClass[] transactionData;
Logger logger = Logger.getLogger("MainNode");
public MainNode(int prevHash, TransactionClass[] transactionData) {
//Logger MainNode Transaction class init
logger.log(Level.INFO,"Node HB Received");
this.prevHash = prevHash;
logger.log(Level.INFO,"Node prevHash loaded : "+prevHash);
this.transactionData = transactionData;
logger.log(Level.INFO,"Node HB Received");
logger.log(Level.INFO,"Node transactionData loaded : "+transactionData);
//Current Calculated blockhash calculated on current transaction data, prev hashcode
logger.log(Level.INFO,"Node Hash Generating ");
Object[] object = new Object[] {transactionData,prevHash};
this.hash = object.hashCode();
logger.log(Level.INFO,"Node Hash Generated : "+ this.hash);
logger.log(Level.INFO,"Node HB Detached");
}
public int getHash() {
return hash;
}
public int getPrevHash() {
return prevHash;
}
public TransactionClass[] getTransactionData() {
return transactionData;
}
}
当交易数据改变时它仍然显示相同的散列
值
这是日志消息
/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=55843:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/tools.jar:/Users/melanrashitha/Desktop/Desktop/stripe-payment-gateway-module/BlockChainArchController/out/production/BlockChainArchController MainNode.pkg.BlockChainMain
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068497599682
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 64c27bf9-d3e6-48bc-b2b5-d6cc10f62df7
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068523666838
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 9796a6c7-9d74-404a-8434-9844c18e481a
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068528416745
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 9469aa4e-70fb-4088-9261-a5274f4214fc
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068533175911
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 1d179fc7-eb39-4c81-bfed-4afdb8579a8f
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Received
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node prevHash loaded : 1
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Received
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node transactionData loaded : [LNodeData.pkg.TransactionClass;@5caf905d
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node Hash Generating `enter code here`
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node Hash Generated : 41359092
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Detached
问题是您在数组上调用 hashCode()
,在您的例子中是 Object[] object
。由于数组不会覆盖对象 class 中的 hashCode()
方法,因此您将从 Object.hashCode()
方法中获得结果。此方法不会考虑数组的内容。因此,在您当前的 JRE 中,您每次都会获得相同的数组哈希码。
String[] data = new String[2];
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
data[0] = "abc";
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
data[1] = "def";
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
这将生成类似于此的输出:
[null, null] - 705927765
[abc, null] - 705927765
[abc, def] - 705927765
如您所见,即使我们更改其内容,哈希码也没有变化。要解决此问题,您可以使用 Arrays.deepHashCode()
方法。它将 return 基于数组内容的哈希码。通过 运行 下面的代码试试看:
String[] data = new String[2];
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
data[0] = "abc";
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
data[1] = "def";
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
您将得到以下输出:
[null, null] - 961
[abc, null] - 2987935
[abc, def] - 3087268
请记住,您的 TransactionClass
class 必须覆盖 hashCode()
方法才能使其正常工作。另请记住,当您覆盖 hashCode()
方法时,您可能还需要覆盖 equals()
方法。
我用 java 创建了简单的区块链程序。目前我正在为交易数据集生成哈希。当我更改交易数据集时,哈希值没有改变
首先,我创建了一个交易 class,其中包含交易名称、交易 ID 和交易时间戳值。目前 transactionDate 和 transactionID 是自动生成的。
这是包裹MainNode.pkg;
import NodeData.pkg.TransactionClass;
import sun.util.logging.PlatformLogger;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author melanrashitha
* @date 11/12/19
* @project BlockChainArchController
*/
public class MainNode {
private int hash;
private int prevHash;
private TransactionClass[] transactionData;
Logger logger = Logger.getLogger("MainNode");
public MainNode(int prevHash, TransactionClass[] transactionData) {
//Logger MainNode Transaction class init
logger.log(Level.INFO,"Node HB Received");
this.prevHash = prevHash;
logger.log(Level.INFO,"Node prevHash loaded : "+prevHash);
this.transactionData = transactionData;
logger.log(Level.INFO,"Node HB Received");
logger.log(Level.INFO,"Node transactionData loaded : "+transactionData);
//Current Calculated blockhash calculated on current transaction data, prev hashcode
logger.log(Level.INFO,"Node Hash Generating ");
Object[] object = new Object[] {transactionData,prevHash};
this.hash = object.hashCode();
logger.log(Level.INFO,"Node Hash Generated : "+ this.hash);
logger.log(Level.INFO,"Node HB Detached");
}
public int getHash() {
return hash;
}
public int getPrevHash() {
return prevHash;
}
public TransactionClass[] getTransactionData() {
return transactionData;
}
}
当交易数据改变时它仍然显示相同的散列 值
这是日志消息
/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=55843:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/lib/tools.jar:/Users/melanrashitha/Desktop/Desktop/stripe-payment-gateway-module/BlockChainArchController/out/production/BlockChainArchController MainNode.pkg.BlockChainMain
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068497599682
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 64c27bf9-d3e6-48bc-b2b5-d6cc10f62df7
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068523666838
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 9796a6c7-9d74-404a-8434-9844c18e481a
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068528416745
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 9469aa4e-70fb-4088-9261-a5274f4214fc
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Recieved
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionDate
INFO: Transaction Date Created : 15068533175911
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass setTransactionID
INFO: TransactionID Created : 1d179fc7-eb39-4c81-bfed-4afdb8579a8f
Nov 12, 2019 11:57:02 AM NodeData.pkg.TransactionClass <init>
INFO: Transaction HB Detached
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Received
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node prevHash loaded : 1
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Received
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node transactionData loaded : [LNodeData.pkg.TransactionClass;@5caf905d
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node Hash Generating `enter code here`
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node Hash Generated : 41359092
Nov 12, 2019 11:57:02 AM MainNode.pkg.MainNode <init>
INFO: Node HB Detached
问题是您在数组上调用 hashCode()
,在您的例子中是 Object[] object
。由于数组不会覆盖对象 class 中的 hashCode()
方法,因此您将从 Object.hashCode()
方法中获得结果。此方法不会考虑数组的内容。因此,在您当前的 JRE 中,您每次都会获得相同的数组哈希码。
String[] data = new String[2];
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
data[0] = "abc";
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
data[1] = "def";
System.out.println(Arrays.toString(data)+" - "+data.hashCode());
这将生成类似于此的输出:
[null, null] - 705927765
[abc, null] - 705927765
[abc, def] - 705927765
如您所见,即使我们更改其内容,哈希码也没有变化。要解决此问题,您可以使用 Arrays.deepHashCode()
方法。它将 return 基于数组内容的哈希码。通过 运行 下面的代码试试看:
String[] data = new String[2];
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
data[0] = "abc";
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
data[1] = "def";
System.out.println(Arrays.toString(data)+" - "+Arrays.deepHashCode(data));
您将得到以下输出:
[null, null] - 961
[abc, null] - 2987935
[abc, def] - 3087268
请记住,您的 TransactionClass
class 必须覆盖 hashCode()
方法才能使其正常工作。另请记住,当您覆盖 hashCode()
方法时,您可能还需要覆盖 equals()
方法。