I think you have the wrong logicMain2.toString
. And Main2.toString
should convert byte[]
to a hexadecimal string.
Here is one implementation:
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
I tried my code, output:
A7FFC6F8BF1ED76651C14756A061D662F580FF4DE43B49FA82D80A4B80F8434A
import org.bouncycastle.jcajce.provider.digest.*;
public class TestSHA3 {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static String sha3(final String input){
String hash = "";
SHA3.DigestSHA3 md = new SHA3.DigestSHA3(256);
md.update(input.getBytes());
hash = bytesToHex(md.digest());
return hash;
}
public static void main(String[] args) {
System.out.println(sha3(""));
}
}
source
share