MessageDigest isEqual() method in Java with Examples

The isEqual() method of java.security.MessageDigest class is used to test if two message digest are equal or not.

Syntax:

public static boolean
  isEqual(byte[] digesta, byte[] digestb)

Paramters: This method takes 2 byte arrays for comparison.



Return Value: This method provides the Boolean value, true if both digests are equal otherwise false.

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

Example 1: For Equal Digest Values

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// isEqual() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
  
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("SHA-256");
  
            // creating object of MessageDigest
            MessageDigest msd2
                = MessageDigest.getInstance("SHA-256");
  
            // getting provider used in object msd
            // using getAlgorithm() method
            boolean status
                = MessageDigest
                      .isEqual(msd1.digest(),
                               msd2.digest());
  
            // check the condition
            if (status)
                System.out.println("msd1 is equals to msd2");
            else
                System.out.println("msd1 is not equals to msd2");
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

msd1 is equals to msd2

Example 2: For Unequal Digest Values

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// isEqual() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
  
            // creating object of MessageDigest
            MessageDigest msd1
                = MessageDigest.getInstance("MD5");
  
            // creating object of MessageDigest
            MessageDigest msd2
                = MessageDigest.getInstance("SHA-256");
  
            // getting provider used in object msd
            // using getAlgorithm() method
            boolean status
                = MessageDigest.isEqual(msd1.digest(),
                                        msd2.digest());
  
            // check the condition
            if (status)
                System.out.println("msd1 is equals to msd2");
            else
                System.out.println("msd1 is not equals to msd2");
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (ProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

msd1 is not equals to msd2

Reference: https://docs.oracle.com/javase/9/docs/api/java/security/MessageDigest.html#isEqual-byte:A-byte:A-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.