summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2019-07-16 14:58:52 -0400
committerFranklin Wei <git@fwei.tk>2019-07-16 14:58:52 -0400
commit56b26148d09574429c232bd9c31738fe7fdbfbef (patch)
treedab03dbc1ee6cb73b5376497e8de3d9d3e71176d
parente5561e6b5a4ef082ab90741652d4d247cfc063b1 (diff)
downloaddcr-56b26148d09574429c232bd9c31738fe7fdbfbef.zip
dcr-56b26148d09574429c232bd9c31738fe7fdbfbef.tar.gz
dcr-56b26148d09574429c232bd9c31738fe7fdbfbef.tar.bz2
dcr-56b26148d09574429c232bd9c31738fe7fdbfbef.tar.xz
iomt: fix leaf hashing bug
This was leaving out the index fields because of how MessageDigest handles ByteBuffers. Fixed by conversion to array first.
-rw-r--r--java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java25
1 files changed, 4 insertions, 21 deletions
diff --git a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java
index 67ac36d..a31c6d9 100644
--- a/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java
+++ b/java-chaincode/dcrchaincode/src/main/java/tk/fwei/dcr/IOMTLeaf.java
@@ -39,7 +39,6 @@ public class IOMTLeaf {
public String hash() {
/* append and hash */
try {
- if(true) {
MessageDigest md = MessageDigest.getInstance("SHA-256");
ByteBuffer buf1, buf2;
@@ -51,30 +50,14 @@ public class IOMTLeaf {
DCRChaincode.LOG.info(IOMT.hexFromBytes(buf1.array()) + " " +
IOMT.hexFromBytes(buf2.array()) + " " + IOMT.hexFromBytes(buf3));
- md.update(buf1);
- md.update(buf2);
+ // must hash array because position index will be at the end of the ByteBuffer:
+ md.update(buf1.array());
+ md.update(buf2.array());
md.update(buf3);
- return IOMT.hexFromBytes(md.digest());
- }
- else {
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- byte buf1[] = {0, 0, 0, 1};
- byte buf2[] = {0, 0, 0, 1};
- byte buf3[] = new byte[64];
-
- Arrays.fill(buf3, (byte) 0x30);
- buf3[63] = (byte) 0x31;
+ DCRChaincode.LOG.info("Hashed "+ (buf1.array().length + buf2.array().length + buf3.length) + " bytes");
- DCRChaincode.LOG.info(IOMT.hexFromBytes(buf1) + " " +
- IOMT.hexFromBytes(buf2) + " " + IOMT.hexFromBytes(buf3));
-
- md.update(buf1);
- md.update(buf1);
- md.update(buf3);
return IOMT.hexFromBytes(md.digest());
- }
-
} catch (Exception e) {
throw new RuntimeException("hash");
}