MD5 算法输出的结果为 128 位长的摘要。当以网络字节序(大端序) 解析时,可得到16字节的二进制数据序列。随后,将这 16 个字节按 base64 算法编码,最终得到可作为 Content-MD5
字段取值的结果。因此,针对 MIME 实体的原始数据应用 MD5 算法。
MD5 作为校验码,是一个 128 位长的二进制数。 在内存中,128 bits = 16 octets。 经过 [Base64][4] 编码,长度增加约 33%,编码后的长度应为 4*⌈16/3⌉ = 24 字节。 而正常编码下的长度为 40 字节。
MD5加密1
此加密为长40字节
public final String getFileMD5String(File file) {
if (file.exists()) {
try {
//获取文件context-MD5
FileInputStream fis = new FileInputStream(file);
String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
IOUtils.closeQuietly(fis);
return md5;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} else {
System.out.println("文件不存在~");
}
return null;
}
MD5加密2
此加密为长24字节
public final String getFileMD5String(File file) {
if (file.exists()) {
try {
//获取文件context-MD5
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(FileUtils.readFileToByteArray(file));
return new String(b);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} else {
System.out.println("文件不存在~");
}
return null;
}
阿里云等 http 请求头 content-Md5 使用为24字节的。
原文链接:https://www.ituring.com.cn/article/74167