"java.security.NoSuchAlgorithmException: SHA3-384 MessageDigest not available" 与 Java 9
"java.security.NoSuchAlgorithmException: SHA3-384 MessageDigest not available" with Java 9
我尝试调用下面提到的 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
没有错误
OpenJdk 9 也应该按照下面的方式实现这个 link。
JEP 287: SHA-3 Hash Algorithms
在您的环境中尝试 运行 下面的代码,看看所有算法都受支持。
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());
}
}
}
不确定,但我认为 link 你已经给了 Oracle 的分数 JDK,而你使用的是 openjdk。
我已经将我的 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)
Oracle JDK 从 https://bugs.openjdk.java.net/browse/JDK-8000415 开始支持 SHA3-384 算法 JDK9。
JDK9 会有此修复。
我尝试调用下面提到的 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
没有错误OpenJdk 9 也应该按照下面的方式实现这个 link。
JEP 287: SHA-3 Hash Algorithms
在您的环境中尝试 运行 下面的代码,看看所有算法都受支持。
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());
}
}
}
不确定,但我认为 link 你已经给了 Oracle 的分数 JDK,而你使用的是 openjdk。
我已经将我的 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)
Oracle JDK 从 https://bugs.openjdk.java.net/browse/JDK-8000415 开始支持 SHA3-384 算法 JDK9。 JDK9 会有此修复。