getInstance(String algorithm)
The getInstance() method of java.security.Provider class is used to return a Signature object that implements the specified signature algorithm.
This method traverses the list of registered security Providers, starting with the most preferred Provider. A new Signature object encapsulating the SignatureSpi implementation from the first Provider that supports the specified algorithm is returned.
Syntax:Â
public static Signature getInstance(String algorithm) throws NoSuchAlgorithmException
Parameters: This method takes the standard name of Algorithm as a parameter.
Return Value: This method returns the new Signature object.
Exception: This method throws NoSuchAlgorithmException if no Provider supports a Signature implementation for the specified algorithm.
Below are the examples to illustrate the getInstance() method:
Example 1:Â Â
Java
// Java program to demonstrate // getInstance() method Â
import java.security.*; import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)     {         try {             // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr = Signature.getInstance( "SHA1WithRSA" ); Â
            // getting the status of signature object             String str = sr.toString(); Â
            // printing the status             System.out.println( "Status : " + str);         } Â
        catch (NoSuchAlgorithmException e) { Â
            System.out.println( "Exception thrown : " + e);         }         catch (ProviderException e) { Â
            System.out.println( "Exception thrown : " + e);         }     } } |
Status : Signature object: SHA1WithRSA
Â
Example 2: To show NoSuchAlgorithmExceptionÂ
Java
// Java program to demonstrate // getInstance() method Â
import java.security.*; import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)     {         try {             // creating the object of Signature and getting instance             // By using getInstance() method             System.out.println( "Trying to get the instance of unknown instance" );             Signature sr = Signature.getInstance( "TAJMAHAL" ); Â
            // getting the status of signature object             String str = sr.toString(); Â
            // printing the status             System.out.println( "Status : " + str);         } Â
        catch (NoSuchAlgorithmException e) { Â
            System.out.println( "Exception thrown : " + e);         }         catch (ProviderException e) { Â
            System.out.println( "Exception thrown : " + e);         }     } } |
Trying to get the instance of unknown instance Exception thrown : java.security.NoSuchAlgorithmException: TAJMAHAL Signature not available
Â
Signature getInstance(String algorithm, Provider provider)
The getInstance() method of java.security.Provider class is used to Returns a Signature object that implements the specified signature algorithm.
A new Signature object encapsulating the SignatureSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.
Syntax:Â Â
public static Signature getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Parameters: This method takes the following arguments as a parameters:Â Â
- algorithm– the name of the algorithm requested.
- provider– the provider
Return Value: This method returns the new Signature object.
Exception: This method throws following exceptions:Â Â
- NoSuchAlgorithmException– if a SignatureSpi implementation for the specified algorithm is not available from the specified Provider object.
- IllegalArgumentException– if the provider is null.
Below are the examples to illustrate the getInstance() method:
Example 1:Â
Java
// Java program to demonstrate // getInstance() method Â
import java.security.*; import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)     {         try {             // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr = Signature.getInstance( "SHA1WithRSA" ); Â
            // creating Provider object             Provider pd = sr.getProvider(); Â
            // getting algorithm name             // by using    getAlgorithm() method             String algo = sr.getAlgorithm(); Â
            // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr1 = Signature.getInstance(algo, pd); Â
            // getting the status of signature object             String str = sr1.toString(); Â
            // printing the status             System.out.println( "Status : " + str);         } Â
        catch (NoSuchAlgorithmException e) { Â
            System.out.println( "Exception thrown : " + e);         }         catch (ProviderException e) { Â
            System.out.println( "Exception thrown : " + e);         }     } } |
Status : Signature object: SHA1WithRSA
Â
Example 2: To show NoSuchAlgorithmException
Java
// Java program to demonstrate // getInstance() method Â
import java.security.*; import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)     {         try {             // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr = Signature.getInstance( "SHA1WithRSA" ); Â
            // creating Provider object             Provider pd = sr.getProvider(); Â
            // getting algorithm name             // by using    getAlgorithm() method             String algo = sr.getAlgorithm(); Â
            // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr1 = Signature.getInstance( "TAJMAHAL" , pd); Â
            // getting the status of signature object             String str = sr1.toString(); Â
            // printing the status             System.out.println( "Status : " + str);         } Â
        catch (NoSuchAlgorithmException e) { Â
            System.out.println( "Exception thrown : " + e);         }         catch (ProviderException e) { Â
            System.out.println( "Exception thrown : " + e);         }     } } |
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: TAJMAHAL for provider SunRsaSign
Â
Example 3: To show IllegalArgumentExceptionÂ
Java
// Java program to demonstrate // getInstance() method Â
import java.security.*; import java.util.*; Â
public class GFG1 {     public static void main(String[] argv)     {         try {             // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr = Signature.getInstance( "SHA1WithRSA" ); Â
            // creating Provider object             Provider pd = null ; Â
            // getting algorithm name             // by using    getAlgorithm() method             String algo = sr.getAlgorithm(); Â
            // creating the object of Signature and getting instance             // By using getInstance() method             Signature sr1 = Signature.getInstance(algo, pd); Â
            // getting the status of signature object             String str = sr1.toString(); Â
            // printing the status             System.out.println( "Status : " + str);         } Â
        catch (NoSuchAlgorithmException e) { Â
            System.out.println( "Exception thrown : " + e);         }         catch (ProviderException e) { Â
            System.out.println( "Exception thrown : " + e);         }         catch (IllegalArgumentException e) { Â
            System.out.println( "Exception thrown : " + e);         }     } } |
Exception thrown : java.lang.IllegalArgumentException: missing provider
Â
[…] of instantiation as for normal class we use a constructor, whereas for singleton class we use the getInstance() method which we will be peeking out in example 1 as depicted below. In general, in order to avoid […]