Sunday, February 8, 2026
HomeLanguagesJavaMessageDigest reset() method in Java with Examples

MessageDigest reset() method in Java with Examples

The reset() method of java.security.MessageDigest class is used to reset current message digest value to default message digest value of this MessageDisgest object.
Syntax: 
 

public void reset()

Return Value: This method has nothing to return.
Below are the examples to illustrate the reset() method:
Example 1: 
 

Java




// Java program to demonstrate
// reset() method
 
import java.security.*;
import java.nio.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
 
            byte[] barr = { 10, 20, 30, 40 };
 
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("MD5");
 
            // display the digest value before Updation
            System.out.println("MessageDigest before update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.wrap(barr);
 
            // update MessageDigest value
            // using update() method
            msd1.update(bb);
 
            // display the digest value before Updation
            System.out.println("\nMessageDigest after update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // reset MessageDigest value
            // using reset() method
            msd1.reset();
 
            // display the digest value after reset
            System.out.println("\nMessageDigest after reset : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

MessageDigest before update : -11235

MessageDigest after update : 30835

MessageDigest after reset : -11235

 

Example 2: 
 

Java




// Java program to demonstrate
// reset() method
 
import java.security.*;
import java.nio.*;
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
 
            byte[] barr = { 10, 20, 30, 40 };
 
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("SHA-256");
 
            // display the digest value before Updation
            System.out.println("MessageDigest before update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.wrap(barr);
 
            // update MessageDigest value
            // using update() method
            msd1.update(bb);
 
            // display the digest value before Updation
            System.out.println("\nMessageDigest after update : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
 
            // reset MessageDigest value
            // using reset() method
            msd1.reset();
 
            // display the digest value after reset
            System.out.println("\nMessageDigest after reset : "
                               + (ByteBuffer.wrap(
                                      msd1.digest()))
                                     .getShort());
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

MessageDigest before update : -7248

MessageDigest after update : 24403

MessageDigest after reset : -7248

 

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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32493 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6864 POSTS0 COMMENTS
Nicole Veronica
11990 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12084 POSTS0 COMMENTS
Shaida Kate Naidoo
7000 POSTS0 COMMENTS
Ted Musemwa
7241 POSTS0 COMMENTS
Thapelo Manthata
6951 POSTS0 COMMENTS
Umr Jansen
6936 POSTS0 COMMENTS