Provider.Service getAlgorithm() method in Java with Examples

The getAlgorithm() method of java.security.Provider.Service class is used to return the standard name of the algorithm this Provider.Service is associated with.
Syntax:Â
public final String getAlgorithm()
Return Value: This method returns name of the algorithm.
Below are the examples to illustrate the getAlgorithm() method:
Example 1:Â Â
Java
// Java program to demonstrate// getAlgorithm() methodÂ
import java.security.*;import java.util.*;Â
public class GFG1 {Â Â Â Â public static void main(String[] argv)Â Â Â Â {Â Â Â Â Â Â Â Â try {Â
            // creating the object of Signature            Signature sr                = Signature.getInstance(                    "SHA1withDSA",                    "SUN");Â
            // getting the Provider of the Signature sr            // by using method getProvider()            Provider provider                = sr.getProvider();Â
            // getting the service of the provider            // using getServices() method            Provider.Service service                = provider                      .getService("Signature",                                  sr.getAlgorithm());Â
            // getting algorithm of Provider.Service object            // by using getAlgorithm() method            String algo = service.getAlgorithm();Â
            // display the result            System.out.println("Algorithm : " + algo);        }Â
        catch (NoSuchAlgorithmException e) {            System.out.println("Exception thrown : " + e);        }        catch (NoSuchProviderException e) {            System.out.println("Exception thrown : " + e);        }    }} |
Output:Â
Algorithm : SHA1withDSA
Â
Example 2:Â
Java
// 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 the Provider of the Signature sr            // by using method getProvider()            Provider provider = msd.getProvider();Â
            // getting the service of the provider            // using getServices() method            Provider.Service service                = provider                      .getService("MessageDigest",                                  msd.getAlgorithm());Â
            // getting algorithm of Provider.Service object            // by using getAlgorithm() method            String algo = service.getAlgorithm();Â
            // display the result            System.out.println("Algorithm : " + algo);        }Â
        catch (NoSuchAlgorithmException e) {            System.out.println("Exception thrown : " + e);        }    }} |
Output:Â
Algorithm : MD5
Â
Reference: https://docs.oracle.com/javase/9/docs/api/java/security/Provider.Service.html#getAlgorithm–
Â



