-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.java
51 lines (41 loc) · 1.45 KB
/
Utils.java
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
49
50
51
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
public class Utils {
private final static String DELIMITER = ":";
public static BigInteger getHash(String nodeKey) {
try {
MessageDigest hasher = MessageDigest.getInstance("SHA-256");
return new BigInteger(hasher.digest(nodeKey.getBytes()));
} catch(NoSuchAlgorithmException e)
{
return null;
}
}
public static boolean isHashcodeBetween(BigInteger hashcode, BigInteger begin, BigInteger end) {
if (end.compareTo(begin) < 0) {
if (hashcode.compareTo(end) > 0 && hashcode.compareTo(begin) > 0) {
return true;
}
if (hashcode.compareTo(end) < 0 && hashcode.compareTo(begin) < 0) {
return true;
}
}
else if (hashcode.compareTo(begin) > 0 && hashcode.compareTo(end) < 0) {
return true;
}
return false;
}
public static String[] decodeNode(String nodeKey) {
return nodeKey.split(DELIMITER);
}
public static String encode_node(String hostname, Integer port) {
return hostname + DELIMITER + port;
}
public static BigInteger getPower(Integer a, int b) {
return (new BigInteger(a.toString()).pow(b));
}
public static BigInteger getModulus(BigInteger a, BigInteger b) {
return a.mod(b);
}
}