getInstance(String algorithm)
java.security.MessageDigest类的getInstance()方法用于返回MessageDigest类型的对象,该对象应用分配的MessageDigest算法。
用法:
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
参数:此方法接受标准算法的名称作为参数。
返回值:此方法提供了MessageDigest类型的对象。
异常:此方法引发以下异常:
- NoSuchAlgorithmException:如果没有提供者支持特定算法的消息摘要spi应用程序。
- NullPointerException :如果algorithm为null。
下面是说明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 usng 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, <initialized>
示例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 usng 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(String algorithm, String provider)
java.security.MessageDigest类的此getInstance()方法提供了MessageDigest类型的对象,该对象应用分配的MessageDigest算法和分配的提供程序对象。
用法:
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException
参数:此方法寻求以下参数作为参数:
- algorithm:是在此实例中要指定的算法的名称。
- provider:这是在此实例中要指定的提供者的名称
返回值:此方法提供了MessageDigest类型的对象。
异常:此方法引发以下异常:
- NoSuchAlgorithmException:–如果特定提供者没有针对特定算法的MessageDigestSpi实现。
- IllegalArgumentException:如果提供者为null。
- 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 usng 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, <initialized>
示例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 usng 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
相关用法
- Java MessageDigest reset()用法及代码示例
- Java MessageDigest isEqual()用法及代码示例
- Java MessageDigest toString()用法及代码示例
- Java MessageDigest getDigestLength()用法及代码示例
- Java MessageDigest getAlgorithm()用法及代码示例
- Java MessageDigest getProvider()用法及代码示例
- Java KeyPairGenerator getInstance()用法及代码示例
- Java Signature getInstance()用法及代码示例
- Java KeyFactory getInstance()用法及代码示例
- Java Calendar getInstance()用法及代码示例
- Java NumberFormat getInstance()用法及代码示例
- Java DecimalFormatSymbols getInstance()用法及代码示例
- Java AlgorithmParameters getInstance()用法及代码示例
- Java SecureRandom getInstance()用法及代码示例
- Java AlgorithmParameterGenerator getInstance()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 MessageDigest getInstance() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。