Open In App

DigestInputStream getMessageDigest() method in Java with Examples

Last Updated : 03 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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–
 



Similar Reads

DigestOutputStream getMessageDigest() method in Java with Examples
The getMessageDigest() method of java.security.DigestOutputStream class provides the MessageDigest assigned to this DigestOutputStream 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
2 min read
DigestInputStream.toString() method in Java with Examples
The toString() method of java.security.DigestInputStream class provide the object of DigestInputStream in string format.Syntax: public String toString() Return Value: This method returns the object of DigestInputStream in string format.Note : All the programs in this article won’t run on online IDE as no ‘name’ file exists. You can check this code
2 min read
DigestInputStream.setMessageDigest() method in Java with Examples
The setMessageDigest() method of java.security.DigestInputStream class used to assign the specified MessageDigest to DigestInputStream object. Syntax: public void setMessageDigest(MessageDigest digest) Return Value: This method has nothing to return. Note: All the programs in this article won’t run on online IDE as no ‘name’ file exists. You can ch
3 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java lang.Long.lowestOneBit() method in Java with Examples
java.lang.Long.lowestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for first set bit present at the lowest position then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(first set bit po
3 min read
Java lang.Long.numberOfTrailingZeros() method in Java with Examples
java.lang.Long.numberOfTrailingZeros() is a built-in function in Java which returns the number of trailing zero bits to the right of the lowest order set bit. In simple terms, it returns the (position-1) where position refers to the first set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it r
3 min read
Java lang.Long.numberOfLeadingZeros() method in Java with Examples
java.lang.Long.numberOfLeadingZeros() is a built-in function in Java which returns the number of leading zero bits to the left of the highest order set bit. In simple terms, it returns the (64-position) where position refers to the highest order set bit from the right. If the number does not contain any set bit(in other words, if the number is zero
3 min read
Java lang.Long.highestOneBit() method in Java with Examples
java.lang.Long.highestOneBit() is a built-in method in Java which first convert the number to Binary, then it looks for the first set bit from the left, then it reset rest of the bits and then returns the value. In simple language, if the binary expression of a number contains a minimum of a single set bit, it returns 2^(last set bit position from
3 min read
Java lang.Long.byteValue() method in Java with Examples
java.lang.Long.byteValue() is a built-in function in Java that returns the value of this Long as a byte. Syntax: public byte byteValue() Parameters: The function does not accept any parameter. Return : This method returns the numeric value represented by this object after conversion to byte type. Examples: Input : 12 Output : 12 Input : 1023 Output
3 min read
Java lang.Long.reverse() method in Java with Examples
java.lang.Long.reverse() is a built-in function in Java which returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified long value. Syntax: public static long reverse(long num) Parameter : num - the number passed Returns : the value obtained by reversing the order of the bits in the
2 min read