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.
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:
import java.security.MessageDigest;
import org.bouncycastle.jcajce.provider.digest.SHA3.DigestSHA3;
import org.bouncycastle.jcajce.provider.digest.SHA3.Digest256;
public class TestSha3 {
public static void main(String[] args) {
System.out.println(sha3(""));
}
public static String sha3(final String input) {
final DigestSHA3 sha3 = new Digest256();
sha3.update(input.getBytes());
return TestSha3.hashToString(sha3);
}
public static String hashToString(MessageDigest hash) {
return hashToString(hash.digest());
}
public static String hashToString(byte[] hash) {
StringBuffer buff = new StringBuffer();
for (byte b : hash) {
buff.append(String.format("%02x", b & 0xFF));
}
return buff.toString();
}
}
a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
I used the following artifact in my Maven build
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.53</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With