use of com.jd.blockchain.crypto.HashFunction in project jdchain-core by blockchain-jd-com.
the class MerkleDataNodeEncoderTest method doTestV1.
private void doTestV1(long sn, long version, Bytes key, byte[] data) {
HashFunction hashFunc = Crypto.getHashFunction(ClassicAlgorithm.SHA256);
HashDigest dataHash = hashFunc.hash(data);
MerkleDataNodeEncoder encoderV1 = new MerkleDataNodeEncoder_V1();
DataNode node = encoderV1.create(ClassicAlgorithm.SHA256.code(), sn, key, version, data);
assertEquals(dataHash, node.getValueHash());
assertEquals(sn, node.getSN());
assertEquals(version, node.getVersion());
assertEquals(key, node.getKey());
byte[] nodeBytes = node.toBytes();
DataNode node_reversed = encoderV1.resolve(nodeBytes);
assertEquals(dataHash, node_reversed.getValueHash());
assertEquals(node.getNodeHash(), node_reversed.getNodeHash());
assertEquals(encoderV1.getFormatVersion(), nodeBytes[0]);
assertEquals(sn, node_reversed.getSN());
assertEquals(version, node_reversed.getVersion());
assertEquals(key, node_reversed.getKey());
}
use of com.jd.blockchain.crypto.HashFunction in project jdchain-core by blockchain-jd-com.
the class PreviousDataNode method newDataNode.
static PreviousDataNode newDataNode(short hashAlgorithm, long sn, Bytes key, long version, byte[] hashedData) {
// byte[] keyStrBytes = BytesUtils.toBytes(key);
// int maskSize = NumberMask.SHORT.getMaskLength(keyStrBytes.length);
int keySize = key.size();
int maskSize = NumberMask.SHORT.getMaskLength(keySize);
// int bodySize = 8 + maskSize + keyStrBytes.length + 8;// sn + key + version;
// sn + key + version;
int bodySize = 8 + maskSize + keySize + 8;
byte[] bodyBytes = new byte[bodySize];
int offset = 0;
offset += BytesUtils.toBytes(sn, bodyBytes, 0);
// NumberMask.SHORT.writeMask(keyStrBytes.length, bodyBytes, offset);
NumberMask.SHORT.writeMask(keySize, bodyBytes, offset);
offset += maskSize;
// System.arraycopy(keyStrBytes, 0, bodyBytes, offset, keyStrBytes.length);
// System.arraycopy(keyStrBytes, 0, bodyBytes, offset, keyStrBytes.length);
// offset += keyStrBytes.length;
offset += key.copyTo(bodyBytes, offset, keySize);
// TODO: version;
offset += BytesUtils.toBytes(version, bodyBytes, offset);
byte[] dataBytes = BytesUtils.concat(bodyBytes, hashedData);
HashFunction hashFunc = Crypto.getHashFunction(hashAlgorithm);
HashDigest dataHash = hashFunc.hash(dataBytes);
int hashMaskSize = NumberMask.TINY.getMaskLength(dataHash.size());
int dataNodeSize = bodySize + hashMaskSize + dataHash.size();
byte[] dataNodeBytes = new byte[dataNodeSize];
offset = 0;
System.arraycopy(bodyBytes, 0, dataNodeBytes, offset, bodySize);
offset += bodySize;
NumberMask.TINY.writeMask(dataHash.size(), dataNodeBytes, offset);
offset += hashMaskSize;
System.arraycopy(dataHash.toBytes(), 0, dataNodeBytes, offset, dataHash.size());
return new PreviousDataNode(sn, key, version, dataHash, dataNodeBytes);
}
use of com.jd.blockchain.crypto.HashFunction in project jdchain-core by blockchain-jd-com.
the class MerkleHashTrieTest method assertMerkleProofEquals.
private void assertMerkleProofEquals(KVEntry data, MerkleProof proof, MerkleProof proof1) {
MerkleProofLevel[] path = proof.getProofLevels();
MerkleProofLevel[] path1 = proof1.getProofLevels();
assertEquals(path.length, path1.length);
for (int i = 0; i < path.length; i++) {
assertEquals(path[i], path1[i]);
}
HashFunction hashFunc = Crypto.getHashFunction(proof.getDataHash().getAlgorithm());
byte[] nodeBytes = BinaryProtocol.encode(data, MerkleTrieData.class);
HashDigest entryHash = hashFunc.hash(nodeBytes);
// 默克尔证明的哈希路径中,最后的两个哈希值分别是 MerkleData 节点的哈希值,和 MerkleData 节点表示的数据的哈希值;
assertEquals(entryHash, path[path.length - 2]);
assertEquals(entryHash, path1[path1.length - 2]);
// TODO: 暂时注释掉这两个
// assertEquals(data.getValue(), proof.getDataHash());
// assertEquals(data.getValue(), proof1.getDataHash());
}
use of com.jd.blockchain.crypto.HashFunction in project jdchain-core by blockchain-jd-com.
the class LedgerRepositoryImpl method innerGetBlock.
private LedgerBlock innerGetBlock(HashDigest blockHash) {
if (blockHash == null) {
return null;
}
Bytes key = encodeBlockStorageKey(blockHash);
// Every one block has only one version;
byte[] blockBytes = versioningStorage.get(key, 0);
if (null == blockBytes) {
return null;
}
LedgerBlockData block;
if (dataStructure.equals(LedgerDataStructure.MERKLE_TREE)) {
block = new LedgerBlockData(deserialize(blockBytes));
} else {
long blockHeight = BytesUtils.toLong(blockBytes);
byte[] blockContent = versioningStorage.get(LEDGER_PREFIX, blockHeight);
block = new LedgerBlockData(deserialize(blockContent));
}
if (!blockHash.equals(block.getHash())) {
throw new RuntimeException("Block hash not equals to it's storage key!");
}
// verify block hash;
byte[] blockBodyBytes = null;
if (block.getHeight() == 0) {
// 计算创世区块的 hash 时,不包括 ledgerHash 字段;
blockBodyBytes = BinaryProtocol.encode(block, BlockBody.class);
} else {
blockBodyBytes = BinaryProtocol.encode(block, BlockBody.class);
}
HashFunction hashFunc = Crypto.getHashFunction(blockHash.getAlgorithm());
boolean pass = hashFunc.verify(blockHash, blockBodyBytes);
if (!pass) {
throw new RuntimeException("Block hash verification fail!");
}
// verify height;
HashDigest indexedHash = getBlockHash(block.getHeight());
if (indexedHash == null || !indexedHash.equals(blockHash)) {
throw new RuntimeException("Illegal ledger state in storage that ledger height index doesn't match it's block data in height[" + block.getHeight() + "] and block hash[" + Base58Utils.encode(blockHash.toBytes()) + "] !");
}
return block;
}
use of com.jd.blockchain.crypto.HashFunction in project jdchain-core by blockchain-jd-com.
the class LedgerAdminDataSetEditor method loadAndVerifySettings.
private LedgerSettings loadAndVerifySettings(HashDigest settingsHash) {
if (settingsHash == null) {
return null;
}
Bytes key = encodeSettingsKey(settingsHash);
byte[] bytes = storage.get(key);
HashFunction hashFunc = Crypto.getHashFunction(settingsHash.getAlgorithm());
if (!hashFunc.verify(settingsHash, bytes)) {
String errorMsg = "Verification of the hash for ledger setting failed! --[HASH=" + key + "]";
LOGGER.error(errorMsg);
throw new LedgerException(errorMsg);
}
return deserializeSettings(bytes);
}
Aggregations