Tuesday, June 16, 2026
HomeLanguagesJavaDigestInputStream getMessageDigest() method in Java with Examples

DigestInputStream getMessageDigest() method in Java with Examples

The getMessageDigest() method of java.security.DigestInputStream class provides the MessageDigest assigned to this DigestInputStream object.

Syntax: 

public MessageDigest getMessageDigest()

Return Value: This method returns MessageDigest object assigned to it.

Note: All the programs in this article won’t run on online IDE as no ‘name’ file exists. You can check this code on Java compiler on your system. To check this code, create a file ‘name’ on your system.

Below are the examples to illustrate the getMessageDigest() method:

Example 1:  

Java




// Java program to demonstrate
// getMessageDigest() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("MD5");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing
            // object of DigestInputStream
            DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the message digest
            // using getMessageDigest() method
            MessageDigest str = di.getMessageDigest();
 
            // display the result
            System.out.println("Status : " + str.toString());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Status : MD5 Message Digest from SUN, 

 

Example 2: 

Java




// Java program to demonstrate
// getMessageDigest() method
 
import java.security.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
            // creating the object of MessageDigest
            // and getting instance
            // By using getInstance() method
            MessageDigest sr
                = MessageDigest.getInstance("SHA-1");
 
            // creating and initializing
            // object of InputStream
            InputStream is
                = new FileInputStream("f:/java/name.txt");
 
            // creating and initializing// object of DigestInputStream
                DigestInputStream di
                = new DigestInputStream(is, sr);
 
            // getting the message digest
            // using getMessageDigest() method
            MessageDigest str = di.getMessageDigest();
 
            // printing the status
            System.out.println("Status : " + str.toString());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (FileNotFoundException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

Status : SHA-1 Message Digest from SUN, 

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/DigestInputStream.html#getMessageDigest–
 

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS