我想使用Java来获得文件的MD5校验和。我真的很惊讶,但我还没有找到任何东西,显示如何获得一个文件的MD5校验和。

这是怎么做到的?


当前回答

public static String getMd5OfFile(String filePath)
{
    String returnVal = "";
    try 
    {
        InputStream   input   = new FileInputStream(filePath); 
        byte[]        buffer  = new byte[1024];
        MessageDigest md5Hash = MessageDigest.getInstance("MD5");
        int           numRead = 0;
        while (numRead != -1)
        {
            numRead = input.read(buffer);
            if (numRead > 0)
            {
                md5Hash.update(buffer, 0, numRead);
            }
        }
        input.close();

        byte [] md5Bytes = md5Hash.digest();
        for (int i=0; i < md5Bytes.length; i++)
        {
            returnVal += Integer.toString( ( md5Bytes[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
    } 
    catch(Throwable t) {t.printStackTrace();}
    return returnVal.toUpperCase();
}

其他回答

String checksum = DigestUtils.md5Hex(new FileInputStream(filePath));

使用Apache Commons Codec库中的DigestUtils:

try (InputStream is = Files.newInputStream(Paths.get("file.zip"))) {
    String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(is);
}

从其他答案中提取想法,这里有一个简单的代码,没有第三方依赖关系(或DatatypeConverter,在最新的jdk中更长),将其生成为与md5sum工具输出兼容的十六进制字符串:

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
...

static String calculateMD5(String path) throws IOException
{
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(Files.readAllBytes(Paths.get(path)));
        return String.format("%032x", new BigInteger(1, md.digest())); // hex, padded to 32 chars
    } catch (NoSuchAlgorithmException ex)
    {
        throw new RuntimeException(ex); // MD5 is always available so this should be impossible
    }
}

我最近不得不为一个动态字符串这样做,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();
}

这显然没有回答你关于如何专门为一个文件做这件事的问题,上面的答案很好地处理了这个问题。我只是花了很多时间使和看起来像大多数应用程序显示它,并认为您可能会遇到同样的麻烦。

有一个输入流装饰器,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();