-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSha3256Hasher.java
More file actions
86 lines (77 loc) · 2.48 KB
/
Sha3256Hasher.java
File metadata and controls
86 lines (77 loc) · 2.48 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package crypto;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import model.codec.EncodedEntity;
import model.crypto.Sha3256Hash;
import model.lightchain.Identifier;
/**
* Implements SHA3-256 hashing functionality.
*/
public class Sha3256Hasher implements Hasher {
private static final String HASH_ALG_SHA_3_256 = "SHA3-256";
private static byte[] concat(final byte[] e1, final byte[] e2) {
byte[] result = new byte[e1.length + e2.length];
System.arraycopy(e1, 0, result, 0, e1.length);
System.arraycopy(e2, 0, result, e1.length, e2.length);
return result;
}
/**
* Computes hash of the given encoded entity.
*
* @param e input encoded entity.
* @return SHA3-256 hash object of the entity.
*/
@Override
public Sha3256Hash computeHash(EncodedEntity e) {
return this.computeHash(e.getBytes());
}
/**
* Computes hash of the given identifier.
*
* @param id input identifier
* @return SHA3-256 hash object of the entity
*/
public Sha3256Hash computeHash(Identifier id) {
return this.computeHash(id.getBytes());
}
/**
* Computes hash of the given bytes.
*
* @param bytes input bytes.
* @return SHA3-256 hash object of the given bytes.
*/
public Sha3256Hash computeHash(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance(HASH_ALG_SHA_3_256);
byte[] hashValue = md.digest(bytes);
return new Sha3256Hash(hashValue);
} catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException(HASH_ALG_SHA_3_256 + "algorithm not found.", ex);
}
}
/**
* Hashing of two given byte arrays.
*
* @param b1 first byte array.
* @param b2 second byte array.
* @return SHA3-256 hash object of the commutative concatenation of the two byte arrays.
*/
public Sha3256Hash computeHash(byte[] b1, byte[] b2) {
try {
MessageDigest md = MessageDigest.getInstance(HASH_ALG_SHA_3_256);
return new Sha3256Hash(md.digest(concat(b1, b2)));
} catch (NoSuchAlgorithmException ex) {
throw new IllegalStateException(HASH_ALG_SHA_3_256 + "algorithm not found.", ex);
}
}
/**
* Hashing of two SHA3-256 hash objects.
*
* @param h1 first SHA3-256 hash object.
* @param h2 second SHA3-256 hash object.
* @return SHA3-256 hash object of the concatenation of the two SHA3-256 hash objects.
*/
public Sha3256Hash computeHash(Sha3256Hash h1, Sha3256Hash h2) {
return computeHash(h1.getBytes(), h2.getBytes());
}
}