Thursday, November 20, 2025
HomeLanguagesJavaMessageDigest getDigestLength() method in Java with Examples

MessageDigest getDigestLength() method in Java with Examples

The getDigestLength() method of MessageDigest class is used to get the size of object of message digest object in bytes.

Syntax:

public final int getDigestLength()

Return Value: This method provides the length of the message digest in byte form.

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

Example 1:




// Java program to demonstrate
// getDigestLength() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
  
            // creating object of MessageDigest
            MessageDigest msd
                = MessageDigest.getInstance("SHA-256", "SUN");
  
            // getting the length of MessageDigest of object msd
            // by using method getDigestLength()
            int length = msd.getDigestLength();
  
            // printing the provider name
            System.out.println("Message digest length : "
                               + length);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (NoSuchProviderException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Message digest length : 32

Example 2:




// Java program to demonstrate
// getDigestLength() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
        try {
  
            // creating object of MessageDigest
            MessageDigest msd
                = MessageDigest.getInstance("MD5");
  
            // getting the length of MessageDigest of object msd
            // by using method getDigestLength()
            int length = msd.getDigestLength();
  
            // printing the provider name
            System.out.println("Message digest length : "
                               + length);
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Message digest length : 16

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6777 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7162 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS