@Test public void testCalcMerkleRoot() { String[][] expected = new String[][]{ new String[]{"1000", "3fa2b684fa9d80f04b70187e6c9ff1c8dd422ce1846beb79cf5e1546c7062d41"}, new String[]{"2000", "4b19aa611413ba9a6b89a2be7833bb835349b9e9e9872c5eacfc82daa2e5f08f"}, new String[]{"3000", "c9ec2ec071ed70d02802decd912a1e8d124420556789384efaab80fcb7ce7ecb"}, new String[]{"4000", "5cfa6745c50787e3d97a1322789713036f8cab7ba534d2a996bea015d811640c"}, new String[]{"5000", "233bc40f24c071507474a9c978f0f0099d0c457f9874326640be55a8a8b96325"}, new String[]{"1024", "5a6c9dcbec66882a3de754eb13e61d8908e6c0b67a23c9d524224ecd93746290"}, new String[]{"2048", "5ee830087937da00520c4ce3793c5c7b951d37771d69a098415ddf7d682a39d9"}, }; for (String[] testCase : expected) { int dataLength = Integer.valueOf(testCase[0]); byte[] data = new byte[dataLength]; for (int i = 0; i < dataLength; i++) { data[i] = (byte) (i / 1024); } assertEquals(testCase[1], NumericUtil.bytesToHex(Hash.merkleHash(data))); } }
public static byte[] merkleHash(byte[] oriData) { if (oriData == null || oriData.length == 0) { throw new IllegalArgumentException("data should not be null"); } int chunkSize = 1024; List<byte[]> hashes = new ArrayList<>(); for (int pos = 0; pos < oriData.length; pos += chunkSize) { int end = Math.min(pos + chunkSize, oriData.length); hashes.add(Sha256Hash.hashTwice(Arrays.copyOfRange(oriData, pos, end))); } int j = 0; for (int size = hashes.size(); size > 1; size = (size + 1) / 2) { for (int i = 0; i < size; i += 2) { int i2 = Math.min(i + 1, size - 1); hashes.add(Sha256Hash.hashTwice(ByteUtil.concat(hashes.get(j + i), hashes.get(j + i2)))); } j += size; } return hashes.get(hashes.size() - 1); }
Hash { public static byte[] merkleHash(byte[] oriData) { if (oriData == null || oriData.length == 0) { throw new IllegalArgumentException("data should not be null"); } int chunkSize = 1024; List<byte[]> hashes = new ArrayList<>(); for (int pos = 0; pos < oriData.length; pos += chunkSize) { int end = Math.min(pos + chunkSize, oriData.length); hashes.add(Sha256Hash.hashTwice(Arrays.copyOfRange(oriData, pos, end))); } int j = 0; for (int size = hashes.size(); size > 1; size = (size + 1) / 2) { for (int i = 0; i < size; i += 2) { int i2 = Math.min(i + 1, size - 1); hashes.add(Sha256Hash.hashTwice(ByteUtil.concat(hashes.get(j + i), hashes.get(j + i2)))); } j += size; } return hashes.get(hashes.size() - 1); } }
Hash { public static byte[] merkleHash(byte[] oriData) { if (oriData == null || oriData.length == 0) { throw new IllegalArgumentException("data should not be null"); } int chunkSize = 1024; List<byte[]> hashes = new ArrayList<>(); for (int pos = 0; pos < oriData.length; pos += chunkSize) { int end = Math.min(pos + chunkSize, oriData.length); hashes.add(Sha256Hash.hashTwice(Arrays.copyOfRange(oriData, pos, end))); } int j = 0; for (int size = hashes.size(); size > 1; size = (size + 1) / 2) { for (int i = 0; i < size; i += 2) { int i2 = Math.min(i + 1, size - 1); hashes.add(Sha256Hash.hashTwice(ByteUtil.concat(hashes.get(j + i), hashes.get(j + i2)))); } j += size; } return hashes.get(hashes.size() - 1); }  }
Hash { public static byte[] merkleHash(byte[] oriData) { if (oriData == null || oriData.length == 0) { throw new IllegalArgumentException("data should not be null"); } int chunkSize = 1024; List<byte[]> hashes = new ArrayList<>(); for (int pos = 0; pos < oriData.length; pos += chunkSize) { int end = Math.min(pos + chunkSize, oriData.length); hashes.add(Sha256Hash.hashTwice(Arrays.copyOfRange(oriData, pos, end))); } int j = 0; for (int size = hashes.size(); size > 1; size = (size + 1) / 2) { for (int i = 0; i < size; i += 2) { int i2 = Math.min(i + 1, size - 1); hashes.add(Sha256Hash.hashTwice(ByteUtil.concat(hashes.get(j + i), hashes.get(j + i2)))); } j += size; } return hashes.get(hashes.size() - 1); }  static String keccak256(String hex); static byte[] keccak256(byte[] input); static byte[] generateMac(byte[] derivedKey, byte[] cipherText); static String sha256(String hexInput); static byte[] sha256(byte[] input); static byte[] hmacSHA256(byte[] key, byte[] data); static byte[] merkleHash(byte[] oriData); }
Hash { public static byte[] merkleHash(byte[] oriData) { if (oriData == null || oriData.length == 0) { throw new IllegalArgumentException("data should not be null"); } int chunkSize = 1024; List<byte[]> hashes = new ArrayList<>(); for (int pos = 0; pos < oriData.length; pos += chunkSize) { int end = Math.min(pos + chunkSize, oriData.length); hashes.add(Sha256Hash.hashTwice(Arrays.copyOfRange(oriData, pos, end))); } int j = 0; for (int size = hashes.size(); size > 1; size = (size + 1) / 2) { for (int i = 0; i < size; i += 2) { int i2 = Math.min(i + 1, size - 1); hashes.add(Sha256Hash.hashTwice(ByteUtil.concat(hashes.get(j + i), hashes.get(j + i2)))); } j += size; } return hashes.get(hashes.size() - 1); }  static String keccak256(String hex); static byte[] keccak256(byte[] input); static byte[] generateMac(byte[] derivedKey, byte[] cipherText); static String sha256(String hexInput); static byte[] sha256(byte[] input); static byte[] hmacSHA256(byte[] key, byte[] data); static byte[] merkleHash(byte[] oriData);  }