@Test public void testCalculateMerkleTreeInPlaceHash() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { OpBlockchainRules bRules = blc.getRules(); Class<OpBlockchainRules> clazz = OpBlockchainRules.class; Method calMerTreeM = clazz.getDeclaredMethod("calculateMerkleTreeInPlaceHash", String.class, List.class); calMerTreeM.setAccessible(true); List<byte[]> inChain = new ArrayList<>(); for (int i = 0; i < 9; i++) { byte[] inData = new byte[200]; new Random().nextBytes(inData); inChain.add(inData); } String rootHash = (String) calMerTreeM.invoke(bRules, SecUtils.HASH_SHA256, inChain); String rootHashDuplicate = (String) calMerTreeM.invoke(bRules, SecUtils.HASH_SHA256, inChain); assertEquals(rootHash, rootHashDuplicate); inChain.get(8)[198] = (byte) (inChain.get(8)[198] >> 1); String rootHash2 = (String) calMerTreeM.invoke(bRules, SecUtils.HASH_SHA256, inChain); assertNotEquals(rootHash, rootHash2); }
private String calculateMerkleTreeInPlaceHash(String algo, List<byte[]> hashes) { if (hashes.size() == 0) { return ""; } if (hashes.size() <= 1) { return SecUtils.formatHashWithAlgo(algo, hashes.get(0)); } List<byte[]> nextLevel = new ArrayList<byte[]>(); for (int i = 0; i < hashes.size(); i += 2) { byte[] hsh = SecUtils.calculateHash(algo, hashes.get(i), i + 1 < hashes.size() ? hashes.get(i + 1) : hashes.get(i)); nextLevel.add(hsh); } return calculateMerkleTreeInPlaceHash(algo, nextLevel); }
OpBlockchainRules { private String calculateMerkleTreeInPlaceHash(String algo, List<byte[]> hashes) { if (hashes.size() == 0) { return ""; } if (hashes.size() <= 1) { return SecUtils.formatHashWithAlgo(algo, hashes.get(0)); } List<byte[]> nextLevel = new ArrayList<byte[]>(); for (int i = 0; i < hashes.size(); i += 2) { byte[] hsh = SecUtils.calculateHash(algo, hashes.get(i), i + 1 < hashes.size() ? hashes.get(i + 1) : hashes.get(i)); nextLevel.add(hsh); } return calculateMerkleTreeInPlaceHash(algo, nextLevel); } }
OpBlockchainRules { private String calculateMerkleTreeInPlaceHash(String algo, List<byte[]> hashes) { if (hashes.size() == 0) { return ""; } if (hashes.size() <= 1) { return SecUtils.formatHashWithAlgo(algo, hashes.get(0)); } List<byte[]> nextLevel = new ArrayList<byte[]>(); for (int i = 0; i < hashes.size(); i += 2) { byte[] hsh = SecUtils.calculateHash(algo, hashes.get(i), i + 1 < hashes.size() ? hashes.get(i + 1) : hashes.get(i)); nextLevel.add(hsh); } return calculateMerkleTreeInPlaceHash(algo, nextLevel); } OpBlockchainRules(JsonFormatter formatter, ValidationListener logValidation); }
OpBlockchainRules { private String calculateMerkleTreeInPlaceHash(String algo, List<byte[]> hashes) { if (hashes.size() == 0) { return ""; } if (hashes.size() <= 1) { return SecUtils.formatHashWithAlgo(algo, hashes.get(0)); } List<byte[]> nextLevel = new ArrayList<byte[]>(); for (int i = 0; i < hashes.size(); i += 2) { byte[] hsh = SecUtils.calculateHash(algo, hashes.get(i), i + 1 < hashes.size() ? hashes.get(i + 1) : hashes.get(i)); nextLevel.add(hsh); } return calculateMerkleTreeInPlaceHash(algo, nextLevel); } OpBlockchainRules(JsonFormatter formatter, ValidationListener logValidation); static boolean validateNickname(String name); static String getRawHash(String hs); static String getSiteFromUser(String name); static String getNicknameFromUser(String name); static String getUserFromNicknameAndSite(String nickname, String site); String calculateMerkleTreeHash(OpBlock op); String calculateSigMerkleTreeHash(OpBlock op); String calculateOperationHash(OpOperation ob, boolean set); int calculateBlockSize(OpBlock cp); JsonFormatter getFormatter(); OpOperation generateHashAndSign(OpOperation op, KeyPair... keyPair); boolean validateRules(OpBlockChain blockchain, OpOperation o, LocalValidationCtx ctx); @SuppressWarnings("unchecked") Map<String, Set<String>> getRoles(OpBlockChain blockchain); boolean validateBlock(OpBlockChain blockChain, OpBlock block, OpBlock prevBlockHeader, boolean validateSignature); boolean validateSignatures(OpBlockChain ctx, OpOperation ob); boolean validateOp(OpBlockChain opBlockChain, OpOperation u, LocalValidationCtx ctx); KeyPair getLoginKeyPair(OpBlockChain ctx, String signedByName, String privateKey); OpObject getLoginKeyObj(OpBlockChain ctx, String signedByName); KeyPair getSignUpKeyPairFromPwd(OpBlockChain blc,
			String name, String pwd); OpBlock createAndSignBlock(Collection<OpOperation> ops, OpBlock prevOpBlockHeader, String serverUser, KeyPair serverKeyPair); static String calculateSuperblockHash(int size, String lastBlockHash); static String calculateHash(OpBlock block); boolean error(OpObject o, ErrorType e, Object... args); boolean error(OpObject o, Exception cause, ErrorType e, Object... args); }
OpBlockchainRules { private String calculateMerkleTreeInPlaceHash(String algo, List<byte[]> hashes) { if (hashes.size() == 0) { return ""; } if (hashes.size() <= 1) { return SecUtils.formatHashWithAlgo(algo, hashes.get(0)); } List<byte[]> nextLevel = new ArrayList<byte[]>(); for (int i = 0; i < hashes.size(); i += 2) { byte[] hsh = SecUtils.calculateHash(algo, hashes.get(i), i + 1 < hashes.size() ? hashes.get(i + 1) : hashes.get(i)); nextLevel.add(hsh); } return calculateMerkleTreeInPlaceHash(algo, nextLevel); } OpBlockchainRules(JsonFormatter formatter, ValidationListener logValidation); static boolean validateNickname(String name); static String getRawHash(String hs); static String getSiteFromUser(String name); static String getNicknameFromUser(String name); static String getUserFromNicknameAndSite(String nickname, String site); String calculateMerkleTreeHash(OpBlock op); String calculateSigMerkleTreeHash(OpBlock op); String calculateOperationHash(OpOperation ob, boolean set); int calculateBlockSize(OpBlock cp); JsonFormatter getFormatter(); OpOperation generateHashAndSign(OpOperation op, KeyPair... keyPair); boolean validateRules(OpBlockChain blockchain, OpOperation o, LocalValidationCtx ctx); @SuppressWarnings("unchecked") Map<String, Set<String>> getRoles(OpBlockChain blockchain); boolean validateBlock(OpBlockChain blockChain, OpBlock block, OpBlock prevBlockHeader, boolean validateSignature); boolean validateSignatures(OpBlockChain ctx, OpOperation ob); boolean validateOp(OpBlockChain opBlockChain, OpOperation u, LocalValidationCtx ctx); KeyPair getLoginKeyPair(OpBlockChain ctx, String signedByName, String privateKey); OpObject getLoginKeyObj(OpBlockChain ctx, String signedByName); KeyPair getSignUpKeyPairFromPwd(OpBlockChain blc,
			String name, String pwd); OpBlock createAndSignBlock(Collection<OpOperation> ops, OpBlock prevOpBlockHeader, String serverUser, KeyPair serverKeyPair); static String calculateSuperblockHash(int size, String lastBlockHash); static String calculateHash(OpBlock block); boolean error(OpObject o, ErrorType e, Object... args); boolean error(OpObject o, Exception cause, ErrorType e, Object... args); static final int MAX_BLOCK_SIZE_OPS; static final int MAX_BLOCKHEADER_SIZE; static final int MAX_ALL_OP_SIZE_MB; static final int MAX_BLOCK_SIZE_MB; static final int MAX_AMOUNT_CREATED_OBJ_FOR_OP; static final int MAX_OP_SIZE_MB; static final int BLOCK_VERSION; static final String OP_TYPE_SYS; static final String OP_LOGIN; static final String OP_SIGNUP; static final String OP_ROLE; static final String OP_GRANT; static final String OP_VALIDATE; static final String OP_VOTE; static final String OP_LIMIT; static final String OP_TABLE; static final String OP_OPERATION; static final String OP_BOT; static final String F_DIGEST; static final String F_ALGO; static final String F_PUBKEY; static final String F_NAME; static final String F_SALT; static final String F_AUTH_METHOD; static final String F_OAUTH_PROVIDER; static final String F_OAUTHID_HASH; static final String F_KEYGEN_METHOD; static final String F_DETAILS; static final String F_TYPE; static final String F_SUPER_ROLES; static final String F_ROLES; static final String F_ERROR_MESSAGE; static final String F_PRIVATEKEY; static final String F_UID; static final String F_VALIDATE; static final String F_IF; static final String METHOD_OAUTH; static final String METHOD_PWD; static final String METHOD_PROVIDED; static final String JSON_MSG_TYPE; static final char USER_LOGIN_CHAR; }