- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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"); }
Advertisements