Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating SHA 3 Hash in Java

Tags:

java

sha-3

People also ask

How is hash calculated in Java?

To calculate cryptographic hashing value in Java, MessageDigest Class is used, under the package java. security. This Algorithms are initialize in static method called getInstance(). After selecting the algorithm it calculate the digest value and return the results in byte array.

Is SHA-3 a hash function?

The new hash algorithm would be referred to as SHA-3. NIST announced the SHA-3 Cryptographic Hash Algorithm Competition on November 2, 2007, and ended the competition on October 2, 2012, when it announced KECCAK as the winning algorithm to be standardized as the new SHA-3.

How do you create a hash in Java?

If you want a cryptographic hash of a String, the Java crypto libraries include implementations of MD5, SHA-1 and so on. You'll typically need to turn the String into a byte array, and then feed that to the hash generator / digest generator.

What is sha256 in Java?

The SHA-256 algorithm generates an almost unique, fixed-size 256-bit (32-byte) hash. This is a one-way function, so the result cannot be decrypted back to the original value. Currently, SHA-2 hashing is widely used, as it is considered the most secure hashing algorithm in the cryptographic arena.


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


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


you can see Implementation in Java

sha3 example