getInstance(String algorithm)
The getInstance() method of java.security.MessageDigest class used to return a object of MessageDigest type that applys 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:
- NoSuchAlgorithmException: if no provider supports an message digest spi application for the particular algorithm.
- NullPointerException: if algorithm is null.
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); } } } |
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); } } } |
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 applys 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:
- algorithm: which is the name of the algorithm to be specified in this instance.
- provider: which is the name of the provider to be specified in this instance
Return Value: This method provides an object of type MessageDigest.
Exception: This method throws following exceptions:
- NoSuchAlgorithmException:– if no MessageDigestSpi implementation for the particular algorithm is available from the particular Provider .
- IllegalArgumentException: if the provider is null.
- NullPointerException: if algorithm is null
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); } } } |
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); } } } |
Exception thrown : java.security.NoSuchAlgorithmException: no such algorithm: GFG for provider SUN
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- MessageDigest isEqual() method in Java with Examples
- MessageDigest reset() method in Java with Examples
- MessageDigest getProvider() method in Java with Examples
- MessageDigest getDigestLength() method in Java with Examples
- MessageDigest toString() method in Java with Examples
- MessageDigest getAlgorithm() method in Java with Examples
- AlgorithmParameters getInstance() method in Java with Examples
- NumberFormat getInstance() method in Java with Examples
- Calendar getInstance() Method in Java with Examples
- KeyPairGenerator getInstance() method in Java with Examples
- SecureRandom getInstance() method in Java with Examples
- Currency getInstance() Method in Java with Examples
- KeyFactory getInstance() method in Java with Examples
- AlgorithmParameterGenerator getInstance() method in Java with Examples
- Java Signature getInstance() method with Examples
- DecimalFormatSymbols getInstance() method in Java with Examples
- DecimalFormatSymbols getInstance(Locale) method in Java with Examples
- Collator getInstance() method in Java with Example
- Collator getInstance(Locale) method in Java with Example
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

