Open In App

MessageDigest getInstance() method in Java with Examples

getInstance(String algorithm)

The getInstance() method of java.security.MessageDigest class used to return a object of MessageDigest type that applies the assigned MessageDigest algorithm. Syntax:

public static MessageDigest
  getInstance(String algorithm)
  throws NoSuchAlgorithmException

Parameters: This method accepts the name of the standard Algorithm as a parameter. Return Value: This method provides an object of type MessageDigest. Exception: This method throws following exception:



Below are the examples to illustrate the getInstance() method: Example 1: 




// 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 MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("MD5");
 
            // getting the status of MessageDigest object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output:

Status : MD5 Message Digest from SUN, <initialized>

Example 2: To show NoSuchAlgorithmException 




// 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 MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("GFG");
 
            // getting the status of MessageDigest object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output:
Exception thrown :
 java.security.NoSuchAlgorithmException:
 GFG MessageDigest not available

getInstance(String algorithm, String provider)

This getInstance() method of java.security.MessageDigest class provides an object of MessageDigest type that applies the assigned MessageDigest algorithm and assigned provider object. Syntax:

public static MessageDigest 
  getInstance(String algorithm, String provider)
  throws NoSuchAlgorithmException

Parameters: This method seeks the following arguments as a parameters:

Return Value: This method provides an object of type MessageDigest. Exception: This method throws following exceptions:

Below are the examples to illustrate the getInstance() method: Example 1: 




// 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 MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance(
                    "SHA-384", "SUN");
 
            // getting the status of MessageDigest object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NoSuchProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output:
Status : SHA-384 Message Digest from SUN, <initialized>

Example 2: To show NoSuchAlgorithmException 




// 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 MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance(
                    "GFG", "SUN");
 
            // getting the status of MessageDigest object
            String str = sr.toString();
 
            // printing the status
            System.out.println("Status : " + str);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NoSuchProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}

Output:
Exception thrown :
 java.security.NoSuchAlgorithmException:
 no such algorithm: GFG for provider SUN

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/MessageDigest.html#getInstance-java.lang.String-java.lang.String-


Article Tags :