use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.
the class LedgerTransactionDataTest method initLedgerTransactionImpl.
@Before
public void initLedgerTransactionImpl() throws Exception {
DataContractRegistry.register(LedgerTransaction.class);
DataContractRegistry.register(LedgerDataSnapshot.class);
DataContractRegistry.register(HashObject.class);
DataContractRegistry.register(DataAccountKVSetOperation.class);
txRequest = initTxRequestMessage(ClassicAlgorithm.SHA256);
long blockHeight = 9986L;
TransactionStagedSnapshot snapshot = initTransactionStagedSnapshot();
data = new TransactionResultData(txRequest.getTransactionHash(), blockHeight, TransactionState.SUCCESS, snapshot, null);
HashDigest hash = ClassicCryptoService.SHA256.hash("zhangsan".getBytes());
HashDigest adminAccountHash = ClassicCryptoService.SHA256.hash("lisi".getBytes());
HashDigest userAccountSetHash = ClassicCryptoService.SHA256.hash("wangwu".getBytes());
HashDigest dataAccountSetHash = ClassicCryptoService.SHA256.hash("zhaoliu".getBytes());
HashDigest contractAccountSetHash = ClassicCryptoService.SHA256.hash("sunqi".getBytes());
snapshot.setAdminAccountHash(adminAccountHash);
snapshot.setUserAccountSetHash(userAccountSetHash);
snapshot.setDataAccountSetHash(dataAccountSetHash);
snapshot.setContractAccountSetHash(contractAccountSetHash);
ledgerTransactionData = new LedgerTransactionData(txRequest, data);
}
use of com.jd.blockchain.crypto.HashDigest in project jdchain-core by blockchain-jd-com.
the class LedgerTransactionDataTest method initTxRequestMessage.
private TxRequestMessage initTxRequestMessage(CryptoAlgorithm hashAlgorithm) throws Exception {
TransactionContent txContent = initTransactionContent();
HashDigest txHash = TxBuilder.computeTxContentHash(hashAlgorithm, txContent);
TxRequestMessage txRequestMessage = new TxRequestMessage(txHash, txContent);
AsymmetricKeypair keypair2 = ClassicCryptoService.ED25519.generateKeypair();
SignatureDigest digest1 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "zhangsan".getBytes());
SignatureDigest digest2 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "lisi".getBytes());
DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob(keypair.getPubKey(), digest1);
DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob(keypair2.getPubKey(), digest2);
txRequestMessage.addEndpointSignatures(endPoint1);
txRequestMessage.addEndpointSignatures(endPoint2);
AsymmetricKeypair keypair4 = ClassicCryptoService.ED25519.generateKeypair();
SignatureDigest digest3 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "wangwu".getBytes());
SignatureDigest digest4 = ClassicCryptoService.ED25519.sign(keypair4.getPrivKey(), "zhaoliu".getBytes());
DigitalSignatureBlob node1 = new DigitalSignatureBlob(keypair.getPubKey(), digest3);
DigitalSignatureBlob node2 = new DigitalSignatureBlob(keypair4.getPubKey(), digest4);
txRequestMessage.addNodeSignatures(node1);
txRequestMessage.addNodeSignatures(node2);
return txRequestMessage;
}
use of com.jd.blockchain.crypto.HashDigest 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.HashDigest 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.HashDigest in project jdchain-core by blockchain-jd-com.
the class MerkleHashDataSetTest method testInsertSameData.
@Test
public void testInsertSameData() {
String keyPrefix = "";
Random rand = new Random();
CryptoSetting cryptoSetting = createCryptoSetting();
MemoryKVStorage storage = new MemoryKVStorage();
MerkleHashDataset mds = new MerkleHashDataset(cryptoSetting, keyPrefix, storage, storage);
Dataset<String, byte[]> ds = DatasetHelper.map(mds);
// 初始的时候没有任何数据,总是返回 null;
DataEntry verKVEntry = ds.getDataEntry("NULL_KEY");
byte[] vbytes = ds.getValue("NULL_KEY");
assertNull(verKVEntry);
assertNull(vbytes);
Map<String, Long> dataVersions = new HashMap<>();
// Map<String, byte[]> dataValues = new HashMap<>();
Map<HashDigest, Map<String, KeySnapshot>> history = new LinkedHashMap<>();
HashDigest rootHash;
// generate base data sample;
// + rand.nextInt(1024);
int count = 1024;
String key;
byte[] data = new byte[64];
rand.nextBytes(data);
long v;
MerkleProof proof;
for (int i = 0; i < count; i++) {
key = "data" + i;
v = ds.setValue(key, data, -1);
dataVersions.put(key, v);
// dataValues.put(key + "_" + v, data);
assertEquals(v, 0);
}
mds.commit();
// Take snapshot;
{
rootHash = mds.getRootHash();
Map<String, KeySnapshot> snapshot = new HashMap<>();
for (int i = 0; i < count; i++) {
key = "data" + i;
// TODO: 暂时注释掉默克尔证明相关的内容;
// proof = mds.getProof(key);
// assertNotNull(proof);
// assertEquals(rootHash, proof.getRootHash());
KeySnapshot ks = new KeySnapshot();
ks.rootHash = mds.getRootHash();
ks.maxVersion = ds.getVersion(key);
snapshot.put(key, ks);
}
history.put(rootHash, snapshot);
}
// verify;
{
MerkleHashDataset mdsReload = new MerkleHashDataset(rootHash, cryptoSetting, keyPrefix, storage, storage, true);
Dataset<String, byte[]> dsReload = DatasetHelper.map(mdsReload);
// verify every keys;
Map<String, KeySnapshot> snapshot = history.get(rootHash);
MerkleProof expProof;
for (int i = 0; i < count; i++) {
key = "data" + i;
// TODO: 暂时注释掉默克尔证明相关的内容;
// proof = mdsReload.getProof(key);
// assertNotNull(proof);
// assertEquals(rootHash, proof.getRootHash());
// expProof = snapshot.get(key).rootHash;
// assertEquals(expProof.toString(), proof.toString());
byte[] value = dsReload.getValue(key);
assertTrue(BytesUtils.equals(data, value));
}
}
}
Aggregations