-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlueprintId.java
70 lines (54 loc) · 1.31 KB
/
BlueprintId.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package buildcraft.builders.blueprints;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import buildcraft.core.network.NetworkData;
public final class BlueprintId {
@NetworkData
public byte[] id;
public static BlueprintId generate(byte[] data) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] id = digest.digest(data);
return new BlueprintId(id);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static BlueprintId fromRawId(byte[] id) {
if (id.length != 32) return null;
return new BlueprintId(id);
}
public BlueprintId() {
}
private BlueprintId(byte[] id) {
this.id = id;
}
public byte[] toRawId() {
return id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BlueprintId) {
return Arrays.equals(id, ((BlueprintId) obj).id);
} else {
return false;
}
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
@Override
public String toString() {
char[] ret = new char[id.length * 2];
for (int i = 0; i < id.length; i++) {
ret[i * 2] = toHex(id[i] >>> 4);
ret[i * 2 + 1] = toHex(id[i] & 0xf);
}
return new String(ret);
}
private char toHex(int i) {
return (char) (i < 10 ? '0' + i : 'a' - 10 + i);
}
}