Java中的 MessageDigest getInstance() 方法及示例
getInstance(字符串算法)
Java.security.MessageDigest类的getInstance()方法用于返回一个 MessageDigest 类型的对象,该对象应用分配的 MessageDigest 算法。
句法:
public static MessageDigest
getInstance(String algorithm)
throws NoSuchAlgorithmException
参数:此方法接受标准算法的名称作为参数。
返回值:此方法提供MessageDigest类型的对象。
异常:此方法抛出以下异常:
- NoSuchAlgorithmException:如果没有提供程序支持特定算法的消息摘要 spi 应用程序。
- NullPointerException:如果算法为空。
下面是说明getInstance()方法的示例:
示例 1:
// Java program to demonstrate
// getInstance() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of MessageDigest
// and getting instance
// By using getInstance() method
MessageDigest sr
= MessageDigest.getInstance("MD5");
// getting the status of MessageDigest object
String str = sr.toString();
// printing the status
System.out.println("Status : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Status : MD5 Message Digest from SUN,
示例 2:显示 NoSuchAlgorithmException
// Java program to demonstrate
// getInstance() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of MessageDigest
// and getting instance
// By using getInstance() method
MessageDigest sr
= MessageDigest.getInstance("GFG");
// getting the status of MessageDigest object
String str = sr.toString();
// printing the status
System.out.println("Status : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Exception thrown :
java.security.NoSuchAlgorithmException:
GFG MessageDigest not available
getInstance(字符串算法,字符串提供者)
Java.security.MessageDigest类的这个getInstance()方法提供了一个 MessageDigest 类型的对象,该对象应用了指定的 MessageDigest 算法和指定的提供者对象。
句法:
public static MessageDigest
getInstance(String algorithm, String provider)
throws NoSuchAlgorithmException
参数:此方法寻求以下参数作为参数:
- algorithm :这是要在此实例中指定的算法的名称。
- provider :这是要在此实例中指定的提供者的名称
返回值:此方法提供MessageDigest类型的对象。
异常:此方法抛出以下异常:
- NoSuchAlgorithmException: – 如果特定 Provider 没有特定算法的 MessageDigestSpi 实现。
- IllegalArgumentException:如果提供者为空。
- NullPointerException:如果算法为空
下面是说明getInstance()方法的示例:
示例 1:
// Java program to demonstrate
// getInstance() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of MessageDigest
// and getting instance
// By using getInstance() method
MessageDigest sr
= MessageDigest.getInstance(
"SHA-384", "SUN");
// getting the status of MessageDigest object
String str = sr.toString();
// printing the status
System.out.println("Status : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Status : SHA-384 Message Digest from SUN,
示例 2:显示 NoSuchAlgorithmException
// Java program to demonstrate
// getInstance() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating the object of MessageDigest
// and getting instance
// By using getInstance() method
MessageDigest sr
= MessageDigest.getInstance(
"GFG", "SUN");
// getting the status of MessageDigest object
String str = sr.toString();
// printing the status
System.out.println("Status : " + str);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (NoSuchProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Exception thrown :
java.security.NoSuchAlgorithmException:
no such algorithm: GFG for provider SUN
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/MessageDigest.html#getInstance-java.lang.String-java.lang.String-