Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaDigestInputStream.setMessageDigest() method in Java with Examples

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 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 setMessageDigest() method:

Example 1:  

Java




// Java program to demonstrate
// setMessageDigest() 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);
 
            // display the result
            System.out.println(
                "MessageDigest before assigning : "
                + di.getMessageDigest());
 
            // assigning the MessageDigest to this stream
            // using setMessageDigest() method
            di.setMessageDigest(
                MessageDigest
                    .getInstance("SHA-1"));
 
            // display the result
            System.out.println(
                "MessageDigest after assigning : "
                + di.getMessageDigest());
        }
 
        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: 

MessageDigest before assigning : MD5 Message Digest from SUN, 

MessageDigest after assigning : SHA-1 Message Digest from SUN, 

 

Example 2: 

Java




// Java program to demonstrate
// setMessageDigest() 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);
 
            // display the result
            System.out.println(
                "MessageDigest before assigning : "
                + di.getMessageDigest());
 
            // assigning the MessageDigest to this stream
            // using setMessageDigest() method
            di.setMessageDigest(
                MessageDigest.getInstance("MD5"));
 
            // display the result
            System.out.println(
                "MessageDigest after assigning : "
                + di.getMessageDigest());
        }
 
        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: 

MessageDigest before assigning : SHA-1 Message Digest from SUN, 

MessageDigest after assigning : MD5 Message Digest from SUN,  

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/DigestInputStream.html#setMessageDigest-java.security.MessageDigest-

RELATED ARTICLES

Most Popular

Recent Comments