Java中的 MessageDigest getProvider() 方法及示例
Java.security.MessageDigest类的getProvider()方法用于获取此消息摘要的提供者。
句法:
public final Provider getProvider()
返回值:此方法返回此 MessageDigest 的提供者。
以下是说明 getProvider() 方法的示例:
示例 1:对于算法MD5
// Java program to demonstrate
// getAlgorithm() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating object of MessageDigest
MessageDigest msd
= MessageDigest.getInstance("MD5");
// getting provider used in object msd
// using getAlgorithm() method
Provider pro = msd.getProvider();
// display the provider
System.out.println("Provider : " + pro);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Provider : SUN version 1.8
示例 2:对于算法SHA-256
// Java program to demonstrate
// getAlgorithm() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
try {
// creating object of MessageDigest
MessageDigest msd
= MessageDigest.getInstance("SHA-256");
// getting provider used in object msd
// using getAlgorithm() method
Provider pro = msd.getProvider();
// display the provider
System.out.println("Provider : " + pro);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Provider : SUN version 1.8
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/MessageDigest.html#getAlgorithm–