Coverage Summary for Class: Keccak256Helper (org.ethereum.crypto)
Class |
Method, %
|
Line, %
|
Keccak256Helper |
15.8%
(3/19)
|
17.1%
(7/41)
|
Keccak256Helper$Size |
0%
(0/3)
|
0%
(0/9)
|
Total |
13.6%
(3/22)
|
14%
(7/50)
|
1 /*
2 * This file is part of RskJ
3 * Copyright (C) 2017 RSK Labs Ltd.
4 * (derived from ethereumJ library, Copyright (c) 2016 <ether.camp>)
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package org.ethereum.crypto;
21
22 import org.bouncycastle.crypto.digests.KeccakDigest;
23 import org.bouncycastle.util.encoders.Hex;
24 import org.ethereum.util.ByteUtil;
25
26 import java.math.BigInteger;
27
28 public class Keccak256Helper {
29
30 public static final int DEFAULT_SIZE = 256;
31 public static final int DEFAULT_SIZE_BYTES = DEFAULT_SIZE / 8;
32
33 public static String keccak256String(String message) {
34 return keccak256String(message, new KeccakDigest(DEFAULT_SIZE), true);
35 }
36
37 public static String keccak256String(byte[] message) {
38 return keccak256String(message, new KeccakDigest(DEFAULT_SIZE), true);
39 }
40
41 public static byte[] keccak256(String message) {
42 return keccak256(Hex.decode(message), new KeccakDigest(DEFAULT_SIZE), true);
43 }
44
45 public static byte[] keccak256(byte[] message) {
46 return keccak256(message, new KeccakDigest(DEFAULT_SIZE), true);
47 }
48
49 public static byte[] keccak256(byte[] message, Size sz) {
50 return keccak256(message, new KeccakDigest(sz.bits), true);
51 }
52
53 public static byte[] keccak256(byte[] m1, byte[] m2) {
54 return keccak256(m1, m2, new KeccakDigest(DEFAULT_SIZE), true);
55 }
56
57 public static byte[] keccak256(byte[] message, int start, int length) {
58 return keccak256(message, start, length, new KeccakDigest(DEFAULT_SIZE), true);
59 }
60
61 protected static String keccak256String(String message, Size bitSize) {
62 KeccakDigest digest = new KeccakDigest(bitSize.bits);
63 return keccak256String(message, digest, true);
64 }
65
66 protected static String keccak256String(byte[] message, Size bitSize) {
67 KeccakDigest digest = new KeccakDigest(bitSize.bits);
68 return keccak256String(message, digest, true);
69 }
70
71 protected static String keccak256String(String message, Size bitSize, boolean bouncyencoder) {
72 KeccakDigest digest = new KeccakDigest(bitSize.bits);
73 return keccak256String(message, digest, bouncyencoder);
74 }
75
76 protected static String keccak256String(byte[] message, Size bitSize, boolean bouncyencoder) {
77 KeccakDigest digest = new KeccakDigest(bitSize.bits);
78 return keccak256String(message, digest, bouncyencoder);
79 }
80
81 private static String keccak256String(String message, KeccakDigest digest, boolean bouncyencoder) {
82 if (message != null) {
83 return keccak256String(Hex.decode(message), digest, bouncyencoder);
84 }
85 throw new NullPointerException("Can't hash a NULL value");
86 }
87
88 private static String keccak256String(byte[] message, KeccakDigest digest, boolean bouncyencoder) {
89 byte[] hash = doKeccak256(message, digest, bouncyencoder);
90 if (bouncyencoder) {
91 return ByteUtil.toHexString(hash);
92 } else {
93 BigInteger bigInt = new BigInteger(1, hash);
94 return bigInt.toString(16);
95 }
96 }
97
98 private static byte[] keccak256(byte[] message, KeccakDigest digest, boolean bouncyencoder) {
99 return doKeccak256(message, digest, bouncyencoder);
100 }
101
102 private static byte[] keccak256(byte[] m1, byte[] m2, KeccakDigest digest, boolean bouncyencoder) {
103 return doKeccak256(m1, m2, digest, bouncyencoder);
104 }
105
106 private static byte[] keccak256(byte[] message, int start, int length, KeccakDigest digest, boolean bouncyencoder) {
107 byte[] hash = new byte[digest.getDigestSize()];
108
109 if (message.length != 0) {
110 digest.update(message, start, length);
111 }
112 digest.doFinal(hash, 0);
113 return hash;
114 }
115
116
117 private static byte[] doKeccak256(byte[] message, KeccakDigest digest, boolean bouncyencoder) {
118 byte[] hash = new byte[digest.getDigestSize()];
119
120 if (message.length != 0) {
121 digest.update(message, 0, message.length);
122 }
123 digest.doFinal(hash, 0);
124 return hash;
125 }
126
127 private static byte[] doKeccak256(byte[] m1, byte[] m2, KeccakDigest digest, boolean bouncyencoder) {
128 byte[] hash = new byte[digest.getDigestSize()];
129 digest.update(m1, 0, m1.length);
130 digest.update(m2, 0, m2.length);
131
132 digest.doFinal(hash, 0);
133 return hash;
134 }
135
136 public enum Size {
137
138 S224(224),
139 S256(256),
140 S384(384),
141 S512(512);
142
143 int bits = 0;
144
145 Size(int bits) {
146 this.bits = bits;
147 }
148
149 public int getValue() {
150 return this.bits;
151 }
152 }
153
154 }