-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSha3256Hash.java
More file actions
72 lines (62 loc) · 1.74 KB
/
Sha3256Hash.java
File metadata and controls
72 lines (62 loc) · 1.74 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package model.crypto;
import java.io.Serializable;
import java.util.Arrays;
import model.lightchain.Identifier;
/**
* Represents SHA3-256 data type which extends abstract Hash data type for
* the cryptographic hash function used in LightChain.
*/
public class Sha3256Hash extends Hash implements Serializable {
public static final int Size = 32;
private final byte[] hashBytes;
/**
* Constructs a SHA3-256 hash object from a byte array.
*
* @param hashValue the byte array to construct the hash from
*/
public Sha3256Hash(byte[] hashValue) {
super(hashValue);
if (hashValue.length != Size) {
throw new IllegalArgumentException("hash value must be 32 bytes long");
}
this.hashBytes = hashValue.clone();
}
/**
* Constructs a SHA3-256 hash object from an identifier.
*
* @param identifier the identifier to construct the hash from
*/
public Sha3256Hash(Identifier identifier) {
super(identifier);
if (identifier.getBytes().length != Size) {
throw new IllegalArgumentException("identifier must be 32 bytes long");
}
this.hashBytes = identifier.getBytes();
}
public byte[] getBytes() {
return hashBytes.clone();
}
@Override
public int compare(Hash other) {
return this.toIdentifier().comparedTo(other.toIdentifier());
}
@Override
public Identifier toIdentifier() {
return new Identifier(this.hashBytes);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Sha3256Hash that = (Sha3256Hash) o;
return Arrays.equals(hashBytes, that.hashBytes);
}
@Override
public int hashCode() {
return Arrays.hashCode(hashBytes);
}
}