我想使用Java来获得文件的MD5校验和。我真的很惊讶,但我还没有找到任何东西,显示如何获得一个文件的MD5校验和。
这是怎么做到的?
我想使用Java来获得文件的MD5校验和。我真的很惊讶,但我还没有找到任何东西,显示如何获得一个文件的MD5校验和。
这是怎么做到的?
当前回答
我们使用的代码类似于前面文章中使用的代码
...
String signature = new BigInteger(1,md5.digest()).toString(16);
...
但是,注意在这里使用BigInteger.toString(),因为它将截断前导零… (例如,尝试s = "27",校验和应该是"02e74f10e0327ad868d138f2b4fdd6f0")
我建议使用Apache Commons Codec,我用它替换了我们自己的代码。
其他回答
public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream("c:\\apache\\cxf.jar");
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Digest(in hex format):: " + sb.toString());
}
或者你可以得到更多的信息 http://www.asjava.com/core-java/java-md5-example/
在Real的Java-How-to中有一个使用MessageDigest类的例子。
请查看该页面中使用CRC32和SHA-1的示例。
import java.io.*;
import java.security.MessageDigest;
public class MD5Checksum {
public static byte[] createChecksum(String filename) throws Exception {
InputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
// see this How-to for a faster way to convert
// a byte array to a HEX string
public static String getMD5Checksum(String filename) throws Exception {
byte[] b = createChecksum(filename);
String result = "";
for (int i=0; i < b.length; i++) {
result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result;
}
public static void main(String args[]) {
try {
System.out.println(getMD5Checksum("apache-tomcat-5.5.17.exe"));
// output :
// 0bb2827c5eacf570b6064e24e0e6653b
// ref :
// http://www.apache.org/dist/
// tomcat/tomcat-5/v5.5.17/bin
// /apache-tomcat-5.5.17.exe.MD5
// 0bb2827c5eacf570b6064e24e0e6653b *apache-tomcat-5.5.17.exe
}
catch (Exception e) {
e.printStackTrace();
}
}
}
有一个输入流装饰器,java。security。DigestInputStream,这样您就可以像往常一样在使用输入流时计算摘要,而不必对数据进行额外的传递。
MessageDigest md = MessageDigest.getInstance("MD5");
try (InputStream is = Files.newInputStream(Paths.get("file.txt"));
DigestInputStream dis = new DigestInputStream(is, md))
{
/* Read decorated stream (dis) to EOF as normal... */
}
byte[] digest = md.digest();
使用Java 7的简单方法,没有第三方库
String path = "your complete file path";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(Files.readAllBytes(Paths.get(path)));
byte[] digest = md.digest();
如果你需要打印这个字节数组。使用方法如下
System.out.println(Arrays.toString(digest));
如果你需要十六进制字符串出这个摘要。使用方法如下
String digestInHex = DatatypeConverter.printHexBinary(digest).toUpperCase();
System.out.println(digestInHex);
其中DatatypeConverter是javax.xml.bind.DatatypeConverter
我最近不得不为一个动态字符串这样做,MessageDigest可以以多种方式表示散列。为了获得文件的签名,就像你会得到md5sum命令,我必须做这样的事情:
try {
String s = "TEST STRING";
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(s.getBytes(),0,s.length());
String signature = new BigInteger(1,md5.digest()).toString(16);
System.out.println("Signature: "+signature);
} catch (final NoSuchAlgorithmException e) {
e.printStackTrace();
}
这显然没有回答你关于如何专门为一个文件做这件事的问题,上面的答案很好地处理了这个问题。我只是花了很多时间使和看起来像大多数应用程序显示它,并认为您可能会遇到同样的麻烦。