28

I have been using CryptoJS (i.e. CryptoJS.algo.SHA3.creat()) library to SHA-3 hash on the front end. (see http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha3.js)

I was wondering if there are any Java library equivalence? But so far I haven't found any. There are not so many Java SHA-3 examples either.

sun.security.Provider has SHA-3, but it's not visible under Eclipse. Also, I am not sure whether this sun's SHA-3 is same as the CryptoJS's SHA-3.

Could anyone please provide some examples?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
gye
  • 1,374
  • 3
  • 16
  • 27
  • 1
    FWIW, you can, in fact, access sun.* classes in Eclipse. For the project where you need to reference the classes, define project-specific Java Build Path -> Libraries and make sure the reference to the "JRE System Library" is both a standalone, Installed JRE (not the JRE in an install JDK) and is referenced as an "Alternate JRE". [Not that I'd recommend doing this...] –  May 07 '15 at 19:42
  • 1
    @jdv The `sun.*` classes are hidden for a reason. You should not use them, or you risk your program being broken on a future Java version. See [Why Developers Should Not Write Programs That Call 'sun' Packages](http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html) – Jesper May 07 '15 at 19:51
  • 1
    @Jesper, I am aware of this, which is why my comments and answer have obvious caveats around them. –  May 07 '15 at 19:53
  • [JEP 287: SHA-3 Hash Algorithms](http://openjdk.java.net/jeps/287) – Ali Dehghani Oct 04 '16 at 20:48

2 Answers2

30

The common Java reference implementation for crypto and crypto support is probably BouncyCastle. It can be a big library to bring in, which is why we often reach into sun.security (rightly or wrongly.)

Anyway, BouncyCastle seems to offer org.bouncycastle.jcajce.provider.digest.SHA3.DigestSHA3

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
27

Thanks to @jdv for his answer. I'm adding more information to have a quick start and an example.

First, add the BouncyCastle maven dependency, or get it on Maven Central :

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.56</version>
    </dependency>

Then we can test it :

import org.bouncycastle.jcajce.provider.digest.SHA3;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

@Test
public void testSha3() throws Exception {
    String input = "Hello world !";
    SHA3.DigestSHA3 digestSHA3 = new SHA3.Digest512();
    byte[] digest = digestSHA3.digest(input.getBytes());

    System.out.println("SHA3-512 = " + Hex.toHexString(digest));
}

You'll get this result (512 bits or 64 bytes in hexadecimal) :

SHA3-512 = e9a4fd120d684537d57f314d51213ce5acef8ff6974e2b7599674ef0f0a3cf111f0d26ed602db946739da448210fb76a7621d7a8f07728372b10a975f36d8e37

You can compare it with this result : https://duckduckgo.com/?q=sha3+%22Hello+world+!%22&ia=answer

Guillaume Husta
  • 4,049
  • 33
  • 40
  • Thanks for answer and howto: I implemented it on java8 without problems. Though in my project on java11 I get an `error: cannot find symbol` on this line: `byte[] digest = digestSHA3.digest(message.getBytes());`. Anybody some experience with bouncy castle and java11? In the mvn repo it writes java5 - java8... – Leder Jan 30 '19 at 15:31
  • Java 11 has digest methods https://docs.oracle.com/en/java/javase/11/docs/api/java.xml.crypto/javax/xml/crypto/dsig/DigestMethod.html –  Feb 26 '19 at 14:47