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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。