Provider getService() method in Java


The service that can describe the implementation of a Provider in regards to any algorithm is obtained using the method getService() in the class java.security.Provider. This method requires two parameters i.e. the service type required and the algorithm name of the required service.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.security.*;
import java.util.*;
public class Demo {
   public static void main(String[] argv) throws Exception {
      try {
         Signature sign = Signature.getInstance("SHA256withDSA");
         Provider p = sign.getProvider();
         Provider.Service s = p.getService("Signature", sign.getAlgorithm());
         System.out.println("The Service for the Provider is: " + s);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

Output

The Service for the Provider is: SUN: Signature.SHA256withDSA -> sun.security.provider.DSA$SHA256withDSA
aliases: [OID.2.16.840.1.101.3.4.3.2, 2.16.840.1.101.3.4.3.2]
attributes: {KeySize=2048, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}

Now let us understand the above program.

The getService() method is used to obtain the service that can describe the implementation of a Provider in regards to any algorithm. Then this is displayed. If the algorithm name is wrong, then the exception NoSuchAlgorithmException is thrown. A code snippet that demonstrates is given as follows −

try {
   Signature sign = Signature.getInstance("SHA256withDSA");
   Provider p = sign.getProvider();
   Provider.Service s = p.getService("Signature", sign.getAlgorithm());
   System.out.println("The Service for the Provider is: " + s);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements