|
1 | | -/* |
2 | | - * Copyright (c) [2016] [ <ether.camp> ] |
3 | | - * This file is part of the ethereumJ library. |
4 | | - * |
5 | | - * The ethereumJ library is free software: you can redistribute it and/or modify |
6 | | - * it under the terms of the GNU Lesser General Public License as published by |
7 | | - * the Free Software Foundation, either version 3 of the License, or |
8 | | - * (at your option) any later version. |
9 | | - * |
10 | | - * The ethereumJ library is distributed in the hope that it will be useful, |
11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | - * GNU Lesser General Public License for more details. |
14 | | - * |
15 | | - * You should have received a copy of the GNU Lesser General Public License |
16 | | - * along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>. |
17 | | - */ |
18 | | - |
19 | | -package org.tron.common.crypto; |
20 | | - |
21 | | -import lombok.extern.slf4j.Slf4j; |
22 | | -import org.tron.common.crypto.jce.TronCastleProvider; |
23 | | -import org.tron.common.utils.ByteArray; |
24 | | -import org.tron.walletserver.WalletApi; |
25 | | - |
26 | | -import java.security.MessageDigest; |
27 | | -import java.security.NoSuchAlgorithmException; |
28 | | -import java.security.Provider; |
29 | | -import java.security.Security; |
30 | | - |
31 | | -import static java.util.Arrays.copyOfRange; |
32 | | - |
33 | | -@Slf4j |
34 | | -public class Hash { |
35 | | - |
36 | | - private static final Provider CRYPTO_PROVIDER; |
37 | | - |
38 | | - private static final String HASH_256_ALGORITHM_NAME; |
39 | | - private static final String HASH_512_ALGORITHM_NAME; |
40 | | - |
41 | | - static { |
42 | | - Security.addProvider(TronCastleProvider.getInstance()); |
43 | | - CRYPTO_PROVIDER = Security.getProvider("SC"); |
44 | | - HASH_256_ALGORITHM_NAME = "TRON-KECCAK-256"; |
45 | | - HASH_512_ALGORITHM_NAME = "TRON-KECCAK-512"; |
46 | | - } |
47 | | - |
48 | | - public static byte[] sha3(byte[] input) { |
49 | | - MessageDigest digest; |
50 | | - try { |
51 | | - digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, |
52 | | - CRYPTO_PROVIDER); |
53 | | - digest.update(input); |
54 | | - return digest.digest(); |
55 | | - } catch (NoSuchAlgorithmException e) { |
56 | | - System.out.println("Can't find such algorithm" + e); |
57 | | - throw new RuntimeException(e); |
58 | | - } |
59 | | - |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * Keccak-256 hash function. |
64 | | - * |
65 | | - * @param hexInput hex encoded input data with optional 0x prefix |
66 | | - * @return hash value as hex encoded string |
67 | | - */ |
68 | | - public static String sha3(String hexInput) { |
69 | | - byte[] bytes = ByteArray.fromHexString(hexInput); |
70 | | - byte[] result = sha3(bytes); |
71 | | - return ByteArray.toHexString(result); |
72 | | - } |
73 | | - |
74 | | - |
75 | | - public static byte[] sha3(byte[] input1, byte[] input2) { |
76 | | - MessageDigest digest; |
77 | | - try { |
78 | | - digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, |
79 | | - CRYPTO_PROVIDER); |
80 | | - digest.update(input1, 0, input1.length); |
81 | | - digest.update(input2, 0, input2.length); |
82 | | - return digest.digest(); |
83 | | - } catch (NoSuchAlgorithmException e) { |
84 | | - System.out.println("Can't find such algorithm" + e); |
85 | | - throw new RuntimeException(e); |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * hashing chunk of the data |
91 | | - * |
92 | | - * @param input - data for hash |
93 | | - * @param start - start of hashing chunk |
94 | | - * @param length - length of hashing chunk |
95 | | - * @return - keccak hash of the chunk |
96 | | - */ |
97 | | - public static byte[] sha3(byte[] input, int start, int length) { |
98 | | - MessageDigest digest; |
99 | | - try { |
100 | | - digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, |
101 | | - CRYPTO_PROVIDER); |
102 | | - digest.update(input, start, length); |
103 | | - return digest.digest(); |
104 | | - } catch (NoSuchAlgorithmException e) { |
105 | | - System.out.println("Can't find such algorithm" + e); |
106 | | - throw new RuntimeException(e); |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - public static byte[] sha512(byte[] input) { |
111 | | - MessageDigest digest; |
112 | | - try { |
113 | | - digest = MessageDigest.getInstance(HASH_512_ALGORITHM_NAME, |
114 | | - CRYPTO_PROVIDER); |
115 | | - digest.update(input); |
116 | | - return digest.digest(); |
117 | | - } catch (NoSuchAlgorithmException e) { |
118 | | - System.out.println("Can't find such algorithm" + e); |
119 | | - throw new RuntimeException(e); |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * Calculates RIGTMOST160(SHA3(input)). This is used in address calculations. * |
125 | | - * |
126 | | - * @param input - data |
127 | | - * @return - add_pre_fix + 20 right bytes of the hash keccak of the data |
128 | | - */ |
129 | | - public static byte[] sha3omit12(byte[] input) { |
130 | | - byte[] hash = sha3(input); |
131 | | - byte[] address = copyOfRange(hash, 11, hash.length); |
132 | | - address[0] = WalletApi.getAddressPreFixByte(); |
133 | | - return address; |
134 | | - } |
135 | | -} |
| 1 | +/* |
| 2 | + * Copyright (c) [2016] [ <ether.camp> ] |
| 3 | + * This file is part of the ethereumJ library. |
| 4 | + * |
| 5 | + * The ethereumJ library is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * The ethereumJ library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.tron.common.crypto; |
| 20 | + |
| 21 | +import static java.util.Arrays.copyOfRange; |
| 22 | + |
| 23 | +import java.security.MessageDigest; |
| 24 | +import java.security.NoSuchAlgorithmException; |
| 25 | +import java.security.Provider; |
| 26 | +import java.security.Security; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.tron.common.crypto.jce.TronCastleProvider; |
| 29 | +import org.tron.common.utils.ByteArray; |
| 30 | +import org.tron.walletserver.WalletApi; |
| 31 | + |
| 32 | +@Slf4j |
| 33 | +public class Hash { |
| 34 | + |
| 35 | + private static final Provider CRYPTO_PROVIDER; |
| 36 | + |
| 37 | + private static final String HASH_256_ALGORITHM_NAME; |
| 38 | + private static final String HASH_512_ALGORITHM_NAME; |
| 39 | + |
| 40 | + static { |
| 41 | + Security.addProvider(TronCastleProvider.getInstance()); |
| 42 | + CRYPTO_PROVIDER = Security.getProvider("BC"); |
| 43 | + HASH_256_ALGORITHM_NAME = "TRON-KECCAK-256"; |
| 44 | + HASH_512_ALGORITHM_NAME = "TRON-KECCAK-512"; |
| 45 | + } |
| 46 | + |
| 47 | + public static byte[] sha3(byte[] input) { |
| 48 | + MessageDigest digest; |
| 49 | + try { |
| 50 | + digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, |
| 51 | + CRYPTO_PROVIDER); |
| 52 | + digest.update(input); |
| 53 | + return digest.digest(); |
| 54 | + } catch (NoSuchAlgorithmException e) { |
| 55 | + System.out.println("Can't find such algorithm" + e); |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Keccak-256 hash function. |
| 63 | + * |
| 64 | + * @param hexInput hex encoded input data with optional 0x prefix |
| 65 | + * @return hash value as hex encoded string |
| 66 | + */ |
| 67 | + public static String sha3(String hexInput) { |
| 68 | + byte[] bytes = ByteArray.fromHexString(hexInput); |
| 69 | + byte[] result = sha3(bytes); |
| 70 | + return ByteArray.toHexString(result); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + public static byte[] sha3(byte[] input1, byte[] input2) { |
| 75 | + MessageDigest digest; |
| 76 | + try { |
| 77 | + digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, |
| 78 | + CRYPTO_PROVIDER); |
| 79 | + digest.update(input1, 0, input1.length); |
| 80 | + digest.update(input2, 0, input2.length); |
| 81 | + return digest.digest(); |
| 82 | + } catch (NoSuchAlgorithmException e) { |
| 83 | + System.out.println("Can't find such algorithm" + e); |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * hashing chunk of the data |
| 90 | + * |
| 91 | + * @param input - data for hash |
| 92 | + * @param start - start of hashing chunk |
| 93 | + * @param length - length of hashing chunk |
| 94 | + * @return - keccak hash of the chunk |
| 95 | + */ |
| 96 | + public static byte[] sha3(byte[] input, int start, int length) { |
| 97 | + MessageDigest digest; |
| 98 | + try { |
| 99 | + digest = MessageDigest.getInstance(HASH_256_ALGORITHM_NAME, |
| 100 | + CRYPTO_PROVIDER); |
| 101 | + digest.update(input, start, length); |
| 102 | + return digest.digest(); |
| 103 | + } catch (NoSuchAlgorithmException e) { |
| 104 | + System.out.println("Can't find such algorithm" + e); |
| 105 | + throw new RuntimeException(e); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public static byte[] sha512(byte[] input) { |
| 110 | + MessageDigest digest; |
| 111 | + try { |
| 112 | + digest = MessageDigest.getInstance(HASH_512_ALGORITHM_NAME, |
| 113 | + CRYPTO_PROVIDER); |
| 114 | + digest.update(input); |
| 115 | + return digest.digest(); |
| 116 | + } catch (NoSuchAlgorithmException e) { |
| 117 | + System.out.println("Can't find such algorithm" + e); |
| 118 | + throw new RuntimeException(e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Calculates RIGTMOST160(SHA3(input)). This is used in address calculations. * |
| 124 | + * |
| 125 | + * @param input - data |
| 126 | + * @return - add_pre_fix + 20 right bytes of the hash keccak of the data |
| 127 | + */ |
| 128 | + public static byte[] sha3omit12(byte[] input) { |
| 129 | + byte[] hash = sha3(input); |
| 130 | + byte[] address = copyOfRange(hash, 11, hash.length); |
| 131 | + address[0] = WalletApi.getAddressPreFixByte(); |
| 132 | + return address; |
| 133 | + } |
| 134 | +} |
0 commit comments