I'm working on a JAVA project that needs to perform a sha3-256 hash. Since Bouncy Castle implemented Sha3 in its latest update, I plan to use their implementation. Here is my code:
public static String sha3(final String input) {
String hash = "";
final SHA3.DigestSHA3 md = new SHA3.DigestSHA3(256);
md.update(input.getBytes());
hash = Main2.toString(md.digest());
return hash;
}
When running System.out.println(Main2.sha3(""));
, I get the following output:
C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470
When I search fot basic sha3 outputs from:
wikipedia: https://en.wikipedia.org/wiki/SHA-3
or
NIST standards: http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA3-256_Msg0.pdf
, it seems I should obtain:
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
Is there any mistake in my code? Any link between bouncy castle's output and NIST's? Would there be a mistake in bouncy castle's implementation?
Thanks for your time and regards.
I guess you have wrong logic of
Main2.toString
. And theMain2.toString
shall to transformbyte[]
to hex string.Here is one implementation:
I have tried your code, the output is :
No need to create custom method to convert byteArray into String, use the inbuilt function
the maven entries are
Your SHA3 should be computed correctly.
You have an issue with the code in your question:
Main2.toString(String)
The following hashes and transforms the bytes into a hexadecimal string:
Output
I used the following artifact in my Maven build
The problem with bouncy castle library versions If you are use bouncy castle 1.50 library(input: SHA3-256("")) output of bouncy does not match with NIST output.
Use bouncy castle 1.54 version Output from bouncy is equal to NIST example values(Example values in https://en.wikipedia.org/wiki/SHA-3 )
Thanks