forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
140 lines (122 loc) · 4.55 KB
/
Hash.java
File metadata and controls
140 lines (122 loc) · 4.55 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.crypto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tron.crypto.jce.TronCastleProvider;
import org.tron.trie.TrieImpl;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import static java.util.Arrays.copyOfRange;
import static org.tron.utils.ByteUtil.EMPTY_BYTE_ARRAY;
public class Hash {
private static final Logger LOG = LoggerFactory.getLogger(Hash.class);
public static final byte[] EMPTY_TRIE_HASH;
private static final Provider CRYPTO_PROVIDER;
private static final String HASH_256_ALGORITHM_NAME;
private static final String HASH_512_ALGORITHM_NAME;
private static final MessageDigest sha256digest;
static {
Security.addProvider(TronCastleProvider.getInstance());
CRYPTO_PROVIDER = Security.getProvider("SC");
HASH_256_ALGORITHM_NAME = "TRON-KECCAK-256";
HASH_512_ALGORITHM_NAME = "TRON-KECCAK-512";
try {
sha256digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
LOG.error("Can't initialize HashUtils", e);
throw new RuntimeException(e); // Can't happen.
}
TrieImpl impl = new TrieImpl();
EMPTY_TRIE_HASH = sha3(impl.encodeElement(EMPTY_BYTE_ARRAY));
}
/**
* @param input - data for hashing
* @return - sha256 hash of the data
*/
public static byte[] sha256(byte[] input) {
return sha256digest.digest(input);
}
public static byte[] sha3(byte[] input) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME,
CRYPTO_PROVIDER);
digest.update(input);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
LOG.error("Can't find such algorithm", e);
throw new RuntimeException(e);
}
}
public static byte[] sha3(byte[] input1, byte[] input2) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME,
CRYPTO_PROVIDER);
digest.update(input1, 0, input1.length);
digest.update(input2, 0, input2.length);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
LOG.error("Can't find such algorithm", e);
throw new RuntimeException(e);
}
}
/**
* hashing chunk of the data
*
* @param input - data for hash
* @param start - start of hashing chunk
* @param length - length of hashing chunk
* @return - keccak hash of the chunk
*/
public static byte[] sha3(byte[] input, int start, int length) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME,
CRYPTO_PROVIDER);
digest.update(input, start, length);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
LOG.error("Can't find such algorithm", e);
throw new RuntimeException(e);
}
}
public static byte[] sha512(byte[] input) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(HASH_512_ALGORITHM_NAME,
CRYPTO_PROVIDER);
digest.update(input);
return digest.digest();
} catch (NoSuchAlgorithmException e) {
LOG.error("Can't find such algorithm", e);
throw new RuntimeException(e);
}
}
/**
* Calculates RIGTMOST160(SHA3(input)). This is used in address
* calculations. *
*
* @param input - data
* @return - 20 right bytes of the hash keccak of the data
*/
public static byte[] sha3omit12(byte[] input) {
byte[] hash = sha3(input);
return copyOfRange(hash, 12, hash.length);
}
}