forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerkleTree.java
More file actions
48 lines (44 loc) · 1.54 KB
/
Copy pathMerkleTree.java
File metadata and controls
48 lines (44 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package DNA.Cryptography;
import java.util.Arrays;
import DNA.UInt256;
/**
* 哈希树
*/
public class MerkleTree {
/**
* 计算根节点的值
* <param name="hashes">子节点列表</param>
* <returns>返回计算的结果</returns>
*/
public static UInt256 computeRoot(UInt256[] hashes) {
if (hashes.length == 0) {
throw new IllegalArgumentException();
}
if (hashes.length == 1) {
return hashes[0];
}
return new UInt256(computeRoot(Arrays.stream(hashes).map(p -> p.toArray()).toArray(byte[][]::new)));
}
private static byte[] computeRoot(byte[][] hashes) {
if (hashes.length == 0) {
throw new IllegalArgumentException();
}
if (hashes.length == 1) {
return hashes[0];
}
if (hashes.length % 2 == 1) {
byte[][] temp = new byte[hashes.length + 1][];
System.arraycopy(hashes, 0, temp, 0, hashes.length);
temp[temp.length - 1] = hashes[hashes.length - 1];
hashes = temp;
}
byte[][] hashes_new = new byte[hashes.length / 2][];
for (int i = 0; i < hashes_new.length; i++) {
byte[] buffer = new byte[hashes[i * 2].length + hashes[i * 2 + 1].length];
System.arraycopy(hashes[i * 2], 0, buffer, 0, hashes[i * 2].length);
System.arraycopy(hashes[i * 2 + 1], 0, buffer, hashes[i * 2].length, hashes[i * 2 + 1].length);
hashes_new[i] = Digest.hash256(buffer);
}
return computeRoot(hashes_new);
}
}