SHA-512 not supported by Java?

23,035

Solution 1

I would say that the most likely explanation is that your REAL code has "SHA_512" rather than "SHA-512". Certainly, that would explain why there is an underscore in the exception message.

The other possibility is that you have a JVM with the "military strength" crypto algorithms removed because of US export restrictions. However, I think that is pretty unlikely ...

(And in fact, my reading of this is that SHA-512 was included in all versions of the default "Sun" provider anyway.)

Solution 2

The following are the standard hashing algorithms provided by the Java MessageDigest:

  • MD2
  • MD5
  • SHA-1
  • SHA-256
  • SHA-384
  • SHA-512

You may want to verify the name you are supplying to the factory method.

Solution 3

Here is the sample method which can be used to get hash string through SHA-512:

private static String getHashCodeFromString(String algorithm, String str) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.update(str.getBytes());
    byte byteData[] = md.digest();

    //convert the byte to hex format method 1
    StringBuffer hashCodeBuffer = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        hashCodeBuffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return hashCodeBuffer.toString();
}

use SHA-512 as algorithm. go to following link for other possible algorithm name you can pass in method. https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#MessageDigest

Solution 4

The MessageDigest class is an engine class designed to provide the functionality of cryptographically secure message digests such as SHA-1 or MD5. A cryptographically secure message digest takes arbitrary-sized input (a byte array), and generates a fixed-size output.

Reference

To print all the MessageDigest provider -

Provider provider[] = Security.getProviders();
for (Provider pro : provider) {
    System.out.println(pro);
    for (Enumeration e = pro.keys(); e.hasMoreElements();)
        System.out.println("\t" + e.nextElement());

}

And fortunatly SHA-512 is there but SHA_512 is not. :)

Solution 5

In Groovy language you can use below method to generate hash string through SHA-512.

It's completely working for me.

public String getHashCodeFromString(String algorithm, String str) throws NoSuchAlgorithmException{
      MessageDigest md = MessageDigest.getInstance(algorithm);
      md.update(str.getBytes());
      def byteData = md.digest() as byte[];

      //convert the byte to hex format method 1
      StringBuffer hashCodeBuffer = new StringBuffer();
      for (int i = 0; i < byteData.length; i++) {
        hashCodeBuffer.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
      }
      return hashCodeBuffer.toString();
    }
Share:
23,035
user236501
Author by

user236501

Updated on July 05, 2022

Comments

  • user236501
    user236501 almost 2 years
    try {
            MessageDigest digest = MessageDigest.getInstance("SHA-512");
            byte[] output = digest.digest(password);
    
            digest.update(salt);
            digest.update(output);
            return new BigInteger(1, digest.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new UnsupportedOperationException(e);
        }
    

    But I got Exception in thread "main" java.security.NoSuchAlgorithmException: SHA_512 MessageDigest not available error

  • Perception
    Perception over 11 years
    Never mind my previous comment. By default Java does not ship with a SHA3 MessageDigest provider. But it looks like BouncyCastle has included it in their library.
  • Admin
    Admin almost 9 years
    Please note that the SHA-384 implemention works by truncating the results from SHA-512, Hence it will require the same amount of calculation as SHA-512
  • Xdg
    Xdg over 8 years
    Neither AES-256 is not included in JDK by default - read knowledge.safe.com/articles/395/… .
  • Garret Wilson
    Garret Wilson about 2 years
    What you list are the standard MessageDigest names. But the document you referenced makes no guarantee that any of these will actually be supported by any particular JDK implementation. In fact as of Java 17, the MessageDigest API still indicates that the JDK is only guaranteed to support SHA-1 and SHA-256. If you have some updated specification that makes further guarantees, please share it.