MessageDigest类的getDigestLength()方法用于获取消息摘要对象的对象大小(以字节为单位)。
用法:
public final int getDigestLength()
返回值:此方法以字节形式提供消息摘要的长度。
下面是说明getDigestLength()方法的示例:
示例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);
}
}
}
输出:
Message digest length : 32
示例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);
}
}
}
输出:
Message digest length : 16