Java中的 MessageDigest getDigestLength() 方法及示例
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
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/MessageDigest.html#getDigestLength–