The Wayback Machine - https://web.archive.org/web/20240716114912/https://www.geeksforgeeks.org/java-signature-getinstance-method-with-examples/
Open In App

Java Signature getInstance() method with Examples

Last Updated : 12 May, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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);
        }
    }
}


Output: 

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);
        }
    }
}


Output: 

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);
        }
    }
}


Output: 

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);
        }
    }
}


Output: 

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);
        }
    }
}


Output: 

Exception thrown : java.lang.IllegalArgumentException: missing provider

 



Similar Reads

KeyPairGenerator getInstance() method in Java with Examples
getInstance(String algorithm) The getInstance() method of java.security.KeyPairGenerator class is used to return a KeyPairGenerator object that generates public/private key pairs for the specified algorithm. This method traverses the list of registered security Providers, starting with the most preferred Provider. A new KeyPairGenerator object enca
5 min read
SecureRandom getInstance() method in Java with Examples
getInstance( String algorithm ) The getInstance() method of java.security.SecureRandom class is used to return a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm. This method traverses the list of registered security Providers, starting with the most preferred Provider. A new SecureRandom object encapsulatin
10 min read
Currency getInstance() Method in Java with Examples
The getInstance() Method of Currency class in Java is used to retrieve the instance of this currency for a given currency code.Syntax: CURRENCY.getInstance(String currency_code) Parameters: This method accepts one parameter currency_code which is the currency of a particular currency.Return Value: This method returns the instance of the currency fo
2 min read
Calendar getInstance() Method in Java with Examples
The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar. Below program illustrates the working of getInstance() Method of Calendar class: E
1 min read
NumberFormat getInstance() method in Java with Examples
The getInstance() method is a built-in method of the java.text.NumberFormat returns a number format for the current default FORMAT locale. Syntax: public static final NumberFormat getInstance() Parameters: The function does not accepts any parameter. Return Value: The function returns the NumberFormat instance for general purpose formatting. Below
2 min read
DecimalFormatSymbols getInstance(Locale) method in Java with Examples
The getInstance(Locale) method of java.text.DecimalFormatSymbols class in Java is used to get the DecimalFormatSymbols instance for the specified Locale. This method is final and cannot be overridden or changed. It takes the Locale for which the DecimalFormatSymbols is required, as a parameter and returns the respective instance of that DecimalForm
1 min read
DecimalFormatSymbols getInstance() method in Java with Examples
The getInstance() method of java.text.DecimalFormatSymbols class in Java is used to get the DecimalFormatSymbols instance for the default Locale. This method is final and cannot be overridden or changed. Syntax: public static final DdecimalFormatSymbols getInstance() Parameter: This method do not accept any parameter. Return Value: This method retu
1 min read
AlgorithmParameters getInstance() method in Java with Examples
getInstance(String algorithm) The getInstance() method of java.security.AlgorithmParameters class returns an object of AlgorithmParameters type that applies the assigned AlgorithmParameters algorithm.Syntax: public static AlgorithmParameters getInstance(String algorithm) throws NoSuchAlgorithmException Parameters: This method seeks the standard Alg
4 min read
AlgorithmParameterGenerator getInstance() method in Java with Examples
getInstance(String algorithm) The getInstance() method of java.security.AlgorithmParameterGenerator class is used to return an object of AlgorithmParameterGenerator type that implements the specified algorithm.Syntax: public static AlgorithmParameterGenerator getInstance(String algorithm) throws NoSuchAlgorithmException Parameters: This method take
4 min read
KeyFactory getInstance() method in Java with Examples
getInstance(String algorithm) The getInstance() method of java.security.KeyFactory class returns a object of KeyFactory type that applies the assigned KeyFactory algorithm. Syntax: public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException Parameters: This method seeks the standard Algorithm as a parameter whose instance
4 min read
three90RightbarBannerImg