[英]How to display hashed MD5 value as text displayed in H2?
您看到[B@48f278eb
的原因是因为生成的 MD5 hash 是非字符串格式的字节。
下面是生成 MD5 hash 并将生成的字节转换为字符串的完整示例,方法是使用 StringBuilder 并将其格式化为String.format("%02x", b)
:
String text = "test";
//Generate MD5 hash
byte[] bytesOfMessage = text.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] arr = md.digest(bytesOfMessage);
//Convert the bytes to a string
StringBuilder strBuilder = new StringBuilder();
for(byte b:arr){
strBuilder.append(String.format("%02x", b));
}
//The complete string
String strHash = strBuilder.toString();
//Print or use the string
System.out.println("The MD of " + text + " is " + strHash);
output 符合预期:
The MD of test is 098f6bcd4621d373cade4e832627b4f6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.