在Java中获取文件的MD5校验和

570

我想使用Java获取文件的MD5校验和。但是我很惊讶,我找不到任何展示如何获取文件的MD5校验和的资料。

请问该怎么做?


1
也许这个链接会有所帮助。你也可以查阅规范,但因为它很复杂,可能需要更多的工作。 - waynecolvin
4
请记住,根据最近的研究,“MD5 应被视为密码学上已被破解且不适合继续使用的算法”。http://en.wikipedia.org/wiki/MD5 - Zakharia Stanley
98
MD5现在不再被认为是安全的密码学算法,但它仍足以用于验证文件完整性,并且比SHA更快。 - jiggy
4
这是关于校验和的问题。 - iPherian
2
MD5校验和在文件中的规范用途是为了避免分发文件被恶意替换。这就是它不安全的地方。但在没有恶意攻击的情况下,它是完全合适的。 - Keith Tyler
22个回答

10
我们在之前的一篇帖子中使用类似上面代码的代码。
...
String signature = new BigInteger(1,md5.digest()).toString(16);
...

注意不要在这里使用BigInteger.toString(),因为它会截断前导零... (例如,尝试s =“27”,校验和应为“02e74f10e0327ad868d138f2b4fdd6f0”

我支持使用Apache Commons编解码器的建议,我用它替换了我们自己的代码。


1
哇,我正在研究一个问题,MD5的东西对于所有的东西都完美地工作,除了一个文件只给我们31个十六进制数字的输出,并且无法通过md5校验。那些前导0的截断真是太痛苦了...感谢你的留言。 - Mike

9

这是一个非常快速和干净的Java方法,不依赖于外部库:

(如果您需要,只需将MD5替换为SHA-1、SHA-256、SHA-384或SHA-512即可。)

public String calcMD5() throws Exception{
        byte[] buffer = new byte[8192];
        MessageDigest md = MessageDigest.getInstance("MD5");

        DigestInputStream dis = new DigestInputStream(new FileInputStream(new File("Path to file")), md);
        try {
            while (dis.read(buffer) != -1);
        }finally{
            dis.close();
        }

        byte[] bytes = md.digest();

        // bytesToHex-method
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }

        return new String(hexChars);
}

1
"bytesToHex" 位可以替换为 new BigInteger(1, bytes).toString(16) - assylias

9

以下是一种方便的方法,它利用了Java 9中的InputStream.transferTo()和Java 11中的OutputStream.nullOutputStream()。它不需要外部库,也不需要将整个文件加载到内存中。

public static String hashFile(String algorithm, File f) throws IOException, NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);

    try(BufferedInputStream in = new BufferedInputStream((new FileInputStream(f)));
        DigestOutputStream out = new DigestOutputStream(OutputStream.nullOutputStream(), md)) {
        in.transferTo(out);
    }

    String fx = "%0" + (md.getDigestLength()*2) + "x";
    return String.format(fx, new BigInteger(1, md.digest()));
}

并且

hashFile("SHA-512", Path.of("src", "test", "resources", "some.txt").toFile());

返回

"e30fa2784ba15be37833d569280e2163c6f106506dfb9b07dde67a24bfb90da65c661110cf2c5c6f71185754ee5ae3fd83a5465c92f72abd888b03187229da29"

9
public static String MD5Hash(String toHash) throws RuntimeException {
   try{
       return String.format("%032x", // produces lower case 32 char wide hexa left-padded with 0
      new BigInteger(1, // handles large POSITIVE numbers 
           MessageDigest.getInstance("MD5").digest(toHash.getBytes())));
   }
   catch (NoSuchAlgorithmException e) {
      // do whatever seems relevant
   }
}

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

6

我在JDK 1.8.0 242中找不到MD5.asHex()方法。 - cbaldan

6

Java标准运行环境方式

public String checksum(File file) {
  try {
    InputStream fin = new FileInputStream(file);
    java.security.MessageDigest md5er =
        MessageDigest.getInstance("MD5");
    byte[] buffer = new byte[1024];
    int read;
    do {
      read = fin.read(buffer);
      if (read > 0)
        md5er.update(buffer, 0, read);
    } while (read != -1);
    fin.close();
    byte[] digest = md5er.digest();
    if (digest == null)
      return null;
    String strDigest = "0x";
    for (int i = 0; i < digest.length; i++) {
      strDigest += Integer.toString((digest[i] & 0xff) 
                + 0x100, 16).substring(1).toUpperCase();
    }
    return strDigest;
  } catch (Exception e) {
    return null;
  }
}

结果等于Linux的md5sum实用程序。

6
这里有一个简单的函数,它将包裹在 Sunil 的代码周围,以便它接受文件作为参数。该函数不需要任何外部库,但需要 Java 7。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;

public class Checksum {

    /**
     * Generates an MD5 checksum as a String.
     * @param file The file that is being checksummed.
     * @return Hex string of the checksum value.
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static String generate(File file) throws NoSuchAlgorithmException,IOException {

        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(Files.readAllBytes(file.toPath()));
        byte[] hash = messageDigest.digest();

        return DatatypeConverter.printHexBinary(hash).toUpperCase();
    }

    public static void main(String argv[]) throws NoSuchAlgorithmException, IOException {
        File file = new File("/Users/foo.bar/Documents/file.jar");          
        String hex = Checksum.generate(file);
        System.out.printf("hex=%s\n", hex);            
    }


}

示例输出:

hex=B117DD0C3CBBD009AC4EF65B6D75C97B

当你使用小文件时,这一切都很好。但对于一个5GB的文件,你需要将其全部读入RAM中,正如文档所述:“它不适用于读取大文件。” - Lorenzo

3
如果您正在使用ANT进行构建,那么这非常简单。只需将以下内容添加到您的build.xml文件中即可:
<checksum file="${jarFile}" todir="${toDir}"/>

jarFile是您想要生成MD5的JAR文件,toDir是您想要放置MD5文件的目录。

更多信息请点击此处。


3

谷歌 guava 提供了一个新的 API。请找到下面的 API:

public static HashCode hash(File file,
            HashFunction hashFunction)
                     throws IOException

Computes the hash code of the file using hashFunction.

Parameters:
    file - the file to read
    hashFunction - the hash function to use to hash the data
Returns:
    the HashCode of all of the bytes in the file
Throws:
    IOException - if an I/O error occurs
Since:
    12.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接