3

我试图调用hash(String plainText)下面提到的函数 -

import java.security.MessageDigest;
import java.lang.Exception;
import java.util.Base64;

public class MyHash {

    private MessageDigest messageDigest = null;
    private String algo = "SHA3-384";

    public String hash(String plainText)
    {
        byte[] hashBytes = null;

        try
        {
            messageDigest = MessageDigest.getInstance(algo);
            messageDigest.update( plainText.getBytes() );
            hashBytes = messageDigest.digest();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return Base64.getEncoder().encodeToString(hashBytes);
   } 
}

并得到这个错误 -

java.security.NoSuchAlgorithmException: SHA3-384 MessageDigest not available
at sun.security.jca.GetInstance.getInstance(java.base@9-internal/GetInstance.java:159)
at java.security.Security.getImpl(java.base@9-internal/Security.java:695)
at java.security.MessageDigest.getInstance(java.base@9-internal/MessageDigest.java:174)
at MyHash.hash(MyHash.java:16)
at Main.main(Main.java:36)

此外,输出java -version-

openjdk version "9-internal"
OpenJDK Runtime Environment (build 9-internal+0-2016-04-14-195246.buildd.src)
OpenJDK 64-Bit Server VM (build 9-internal+0-2016-04-14-195246.buildd.src, mixed mode)

不知道为什么我会收到此错误。任何帮助将不胜感激。

编辑 1:

这些是支持的算法 -

    Provider: SUN
  Algorithm: SHA1PRNG
  Algorithm: NativePRNG
  Algorithm: NativePRNGBlocking
  Algorithm: NativePRNGNonBlocking
  Algorithm: SHA1withDSA
  Algorithm: NONEwithDSA
  Algorithm: SHA224withDSA
  Algorithm: SHA256withDSA
  Algorithm: SHA1withDSAinP1363Format
  Algorithm: NONEwithDSAinP1363Format
  Algorithm: SHA224withDSAinP1363Format
  Algorithm: SHA256withDSAinP1363Format
  Algorithm: DSA
  Algorithm: MD2
  Algorithm: MD5
  Algorithm: SHA
  Algorithm: SHA-224
  Algorithm: SHA-256
  Algorithm: SHA-384
  Algorithm: SHA-512
  Algorithm: DSA
  Algorithm: DSA
  Algorithm: DSA
  Algorithm: X.509
  Algorithm: PKCS12
  Algorithm: JKS
  Algorithm: CaseExactJKS
  Algorithm: DKS
  Algorithm: JavaPolicy
  Algorithm: JavaLoginConfig
  Algorithm: PKIX
  Algorithm: PKIX
  Algorithm: Collection
  Algorithm: com.sun.security.IndexedCollection
Provider: SunRsaSign
  Algorithm: RSA
  Algorithm: RSA
  Algorithm: MD2withRSA
  Algorithm: MD5withRSA
  Algorithm: SHA1withRSA
  Algorithm: SHA224withRSA
  Algorithm: SHA256withRSA
  Algorithm: SHA384withRSA
  Algorithm: SHA512withRSA
Provider: SunJSSE
  Algorithm: RSA
  Algorithm: RSA
  Algorithm: MD2withRSA
  Algorithm: MD5withRSA
  Algorithm: SHA1withRSA
  Algorithm: MD5andSHA1withRSA
  Algorithm: SunX509
  Algorithm: NewSunX509
  Algorithm: SunX509
  Algorithm: PKIX
  Algorithm: TLSv1
  Algorithm: TLSv1.1
  Algorithm: TLSv1.2
  Algorithm: TLS
  Algorithm: DTLSv1.0
  Algorithm: DTLSv1.2
  Algorithm: DTLS
  Algorithm: Default
  Algorithm: PKCS12
Provider: SunJCE
  Algorithm: RSA
  Algorithm: DES
  Algorithm: DESede
  Algorithm: DESedeWrap
  Algorithm: PBEWithMD5AndDES
  Algorithm: PBEWithMD5AndTripleDES
  Algorithm: PBEWithSHA1AndDESede
  Algorithm: PBEWithSHA1AndRC2_40
  Algorithm: PBEWithSHA1AndRC2_128
  Algorithm: PBEWithSHA1AndRC4_40
  Algorithm: PBEWithSHA1AndRC4_128
  Algorithm: PBEWithHmacSHA1AndAES_128
  Algorithm: PBEWithHmacSHA224AndAES_128
  Algorithm: PBEWithHmacSHA256AndAES_128
  Algorithm: PBEWithHmacSHA384AndAES_128
  Algorithm: PBEWithHmacSHA512AndAES_128
  Algorithm: PBEWithHmacSHA1AndAES_256
  Algorithm: PBEWithHmacSHA224AndAES_256
  Algorithm: PBEWithHmacSHA256AndAES_256
  Algorithm: PBEWithHmacSHA384AndAES_256
  Algorithm: PBEWithHmacSHA512AndAES_256
  Algorithm: Blowfish
  Algorithm: AES
  Algorithm: AES_128/ECB/NoPadding
  Algorithm: AES_128/CBC/NoPadding
  Algorithm: AES_128/OFB/NoPadding
  Algorithm: AES_128/CFB/NoPadding
  Algorithm: AES_128/GCM/NoPadding
  Algorithm: AES_192/ECB/NoPadding
  Algorithm: AES_192/CBC/NoPadding
  Algorithm: AES_192/OFB/NoPadding
  Algorithm: AES_192/CFB/NoPadding
  Algorithm: AES_192/GCM/NoPadding
  Algorithm: AES_256/ECB/NoPadding
  Algorithm: AES_256/CBC/NoPadding
  Algorithm: AES_256/OFB/NoPadding
  Algorithm: AES_256/CFB/NoPadding
  Algorithm: AES_256/GCM/NoPadding
  Algorithm: AESWrap
  Algorithm: AESWrap_128
  Algorithm: AESWrap_192
  Algorithm: AESWrap_256
  Algorithm: RC2
  Algorithm: ARCFOUR
  Algorithm: DES
  Algorithm: DESede
  Algorithm: Blowfish
  Algorithm: AES
  Algorithm: RC2
  Algorithm: ARCFOUR
  Algorithm: HmacMD5
  Algorithm: HmacSHA1
  Algorithm: HmacSHA224
  Algorithm: HmacSHA256
  Algorithm: HmacSHA384
  Algorithm: HmacSHA512
  Algorithm: DiffieHellman
  Algorithm: DiffieHellman
  Algorithm: DiffieHellman
  Algorithm: DiffieHellman
  Algorithm: DES
  Algorithm: DESede
  Algorithm: PBE
  Algorithm: PBEWithMD5AndDES
  Algorithm: PBEWithMD5AndTripleDES
  Algorithm: PBEWithSHA1AndDESede
  Algorithm: PBEWithSHA1AndRC2_40
  Algorithm: PBEWithSHA1AndRC2_128
  Algorithm: PBEWithSHA1AndRC4_40
  Algorithm: PBEWithSHA1AndRC4_128
  Algorithm: PBES2
  Algorithm: PBEWithHmacSHA1AndAES_128
  Algorithm: PBEWithHmacSHA224AndAES_128
  Algorithm: PBEWithHmacSHA256AndAES_128
  Algorithm: PBEWithHmacSHA384AndAES_128
  Algorithm: PBEWithHmacSHA512AndAES_128
  Algorithm: PBEWithHmacSHA1AndAES_256
  Algorithm: PBEWithHmacSHA224AndAES_256
  Algorithm: PBEWithHmacSHA256AndAES_256
  Algorithm: PBEWithHmacSHA384AndAES_256
  Algorithm: PBEWithHmacSHA512AndAES_256
  Algorithm: Blowfish
  Algorithm: AES
  Algorithm: GCM
  Algorithm: RC2
  Algorithm: OAEP
  Algorithm: DiffieHellman
  Algorithm: DES
  Algorithm: DESede
  Algorithm: PBEWithMD5AndDES
  Algorithm: PBEWithMD5AndTripleDES
  Algorithm: PBEWithSHA1AndDESede
  Algorithm: PBEWithSHA1AndRC2_40
  Algorithm: PBEWithSHA1AndRC2_128
  Algorithm: PBEWithSHA1AndRC4_40
  Algorithm: PBEWithSHA1AndRC4_128
  Algorithm: PBEWithHmacSHA1AndAES_128
  Algorithm: PBEWithHmacSHA224AndAES_128
  Algorithm: PBEWithHmacSHA256AndAES_128
  Algorithm: PBEWithHmacSHA384AndAES_128
  Algorithm: PBEWithHmacSHA512AndAES_128
  Algorithm: PBEWithHmacSHA1AndAES_256
  Algorithm: PBEWithHmacSHA224AndAES_256
  Algorithm: PBEWithHmacSHA256AndAES_256
  Algorithm: PBEWithHmacSHA384AndAES_256
  Algorithm: PBEWithHmacSHA512AndAES_256
  Algorithm: PBKDF2WithHmacSHA1
  Algorithm: PBKDF2WithHmacSHA224
  Algorithm: PBKDF2WithHmacSHA256
  Algorithm: PBKDF2WithHmacSHA384
  Algorithm: PBKDF2WithHmacSHA512
  Algorithm: HmacMD5
  Algorithm: HmacSHA1
  Algorithm: HmacSHA224
  Algorithm: HmacSHA256
  Algorithm: HmacSHA384
  Algorithm: HmacSHA512
  Algorithm: HmacPBESHA1
  Algorithm: PBEWithHmacSHA1
  Algorithm: PBEWithHmacSHA224
  Algorithm: PBEWithHmacSHA256
  Algorithm: PBEWithHmacSHA384
  Algorithm: PBEWithHmacSHA512
  Algorithm: SslMacMD5
  Algorithm: SslMacSHA1
  Algorithm: JCEKS
  Algorithm: SunTlsPrf
  Algorithm: SunTls12Prf
  Algorithm: SunTlsMasterSecret
  Algorithm: SunTlsKeyMaterial
  Algorithm: SunTlsRsaPremasterSecret

编辑 2:

Oracle 的 JDK 9.0.1 没有错误

4

3 回答 3

5

OpenJdk 9 也应该按照下面的链接实现这个。

JEP 287:SHA-3 哈希算法

尝试在您的环境中运行以下代码,看看所有算法都受支持。

 public static void main(String[] args) {

                for (Provider provider : Security.getProviders()) {
                    System.out.println("Provider: " + provider.getName());
                    for (Provider.Service service : provider.getServices()) {
                        System.out.println("  Algorithm: " + service.getAlgorithm());
                    }
                }


    }

不确定,但我认为您给出的链接指向 Oracle 的 JDK,而您使用的是 openjdk。

于 2017-12-31T07:11:01.897 回答
1

我已将我的 JDK 更新为 Oracle 的 JDK 9.0.1,并且可以正常使用SHA3-384而不会出现任何错误。更新后的输出java -version-

java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
于 2017-12-31T13:46:49.120 回答
1

Oracle JDK 从 JDK9 中的https://bugs.openjdk.java.net/browse/JDK-8000415开始支持 SHA3-384 算法。JDK9 将进行此修复。

于 2018-01-05T13:53:36.620 回答