Java中的 MessageDigest toString() 方法及示例
Java.security.MessageDigest类的toString()方法用于提供字符串格式的消息摘要对象。
句法:
public String toString()
返回值:该方法以字符串格式返回消息摘要的对象。
下面是说明toString()方法的示例:
示例 1:
// Java program to demonstrate
// toString() 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");
// getting the string value of msd1
// using toString() method
String nv = msd1.toString();
// display the result
System.out.println("MessageDigest : "
+ nv);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : "
+ e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : "
+ e);
}
}
}
输出:
MessageDigest : MD5 Message Digest from SUN,
示例 2:
// Java program to demonstrate
// toString() 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");
// getting the string value of msd1
// using toString() method
String nv = msd1.toString();
// display the result
System.out.println("MessageDigest : " + nv);
}
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown : " + e);
}
catch (ProviderException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
MessageDigest : SHA-256 Message Digest from SUN,
参考: https://docs.oracle.com/javase/9/docs/api/ Java/security/MessageDigest.html#toString–