Java中的提供者 getService() 和 getServices() 方法
getService(字符串类型,字符串算法)
Java.security.Provider类的getService()方法用于获取描述该Provider对该算法或别名的指定类型的实现的服务。
如果不存在这样的实现,则此方法返回 null。如果有两个匹配的服务,一个使用 putService() 添加到此提供程序,一个通过 put() 添加,则返回通过 putService() 添加的服务。
句法:
public Provider.Service getService(String type, String algorithm)
参数:此方法将以下参数作为参数。
- type – 请求的服务类型。
- algorithm – 请求的服务的不区分大小写的算法名称(或备用别名)。
返回值:此方法返回描述此 Provider 的匹配服务的服务,如果不存在此类服务,则返回null 。
异常:如果类型或算法为空,此方法将抛出NullPointerException 。
以下是说明getService()方法的示例:
示例 1:
// Java program to demonstrate
// getService() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of SecureRandom
Signature sr = Signature.getInstance("SHA1withDSA", "SUN");
// getting the Provider of the SecureRandom sr
// by using method getProvider()
Provider provider = sr.getProvider();
// getting the service of the provider using getServices() method
Provider.Service service1 = provider
.getService("Signature", sr.getAlgorithm());
// printing the service
System.out.println("Provider service : " + service1);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Provider service : SUN: Signature.SHA1withDSA -> sun.security.provider.DSA$SHA1withDSA
aliases: [DSA, DSS, SHA/DSA, SHA-1/DSA, SHA1/DSA, SHAwithDSA, DSAWithSHA1, OID.1.2.840.10040.4.3, 1.2.840.10040.4.3, 1.3.14.3.2.13, 1.3.14.3.2.27]
attributes: {ImplementedIn=Software, KeySize=1024, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}
示例 2:显示由 getService() 方法抛出的 NullPointerException。
// Java program to demonstrate
// getService() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of SecureRandom
Signature sr = Signature.getInstance("SHA1withDSA", "SUN");
// getting the Provider of the SecureRandom sr
// by using method getProvider()
Provider provider = sr.getProvider();
// getting the service of the provider using getServices() method
Provider.Service service1 = provider
.getService(null, sr.getAlgorithm());
// printing the service
System.out.println("Provider service : " + service1);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
Exception thrown : java.lang.NullPointerException
获取服务()
Java.security.Provider类的getServices()方法用于获取此 Provider 支持的所有服务的不可修改 Set。
句法:
public Set getServices()
返回值:此方法返回此 Provider 支持的所有服务的不可修改的 Set 。
下面是说明 getServices() 方法的示例:
方案一:
// Java program to demonstrate
// getService() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
// Declaring int variable
int i = 5;
try {
// creating the object of SecureRandom
Signature sr = Signature.getInstance("SHA1withDSA", "SUN");
// getting the Provider of the SecureRandom sr
// by using method getProvider()
Provider provider = sr.getProvider();
// Declaring the variable of set
输出:
Provider servicelist :
Value is : SUN: SecureRandom.NativePRNG -> sun.security.provider.NativePRNG
Value is : SUN: SecureRandom.SHA1PRNG -> sun.security.provider.SecureRandom
attributes: {ImplementedIn=Software}
Value is : SUN: SecureRandom.NativePRNGBlocking -> sun.security.provider.NativePRNG$Blocking
Value is : SUN: SecureRandom.NativePRNGNonBlocking -> sun.security.provider.NativePRNG$NonBlocking
Value is : SUN: Signature.SHA1withDSA -> sun.security.provider.DSA$SHA1withDSA
aliases: [DSA, DSS, SHA/DSA, SHA-1/DSA, SHA1/DSA, SHAwithDSA, DSAWithSHA1, OID.1.2.840.10040.4.3, 1.2.840.10040.4.3, 1.3.14.3.2.13, 1.3.14.3.2.27]
attributes: {ImplementedIn=Software, KeySize=1024, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}