java - 如何在 Android/Java 中获取文件目录的 MD5 校验和

标签 java android md5 checksum

我想获取文件目录的MD5校验和,我已经获取了文件的算法,但是当我将它用于目录时,结果为空。如何快速获取校验和。 以下是我的文件算法(由匿名 stackoverflower 编辑)。

public String fileToMD5(String filePath) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath); // Create an FileInputStream instance according to the filepath
            byte[] buffer = new byte[1024]; // The buffer to read the file
            MessageDigest digest = MessageDigest.getInstance("MD5"); // Get a MD5 instance
            int numRead = 0; // Record how many bytes have been read
            while (numRead != -1) {
                numRead = inputStream.read(buffer);
                if (numRead > 0)
                    digest.update(buffer, 0, numRead); // Update the digest
            }
            byte [] md5Bytes = digest.digest(); // Complete the hash computing
            return convertHashToString(md5Bytes); // Call the function to convert to hex digits
        } catch (Exception e) {
            return null;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close(); // Close the InputStream
                } catch (Exception e) { }
            }
        }
    }

我搜索了一下,有一些解决方案:

  1. 对目录下的文件进行预排序。
  2. 将目录压缩为存档文件(例如 .zip 或 .rar),并对其进行校验和。
  3. 将目录下的所有内容获取到一个流中,并进行校验。

不知道是否有一些方便的解决方案。先谢谢您了。

最佳答案

您需要: 1.计算目录下所有文件的md5 2.计算1步结果的md5。

这是给你的

public static String dirMD5(String dir)
{
    String md5    = "";
    File   folder = new File(dir);
    File[] files  = folder.listFiles();

    for (int i=0; i<files.length; i++)
    {
        md5 = md5 + getMd5OfFile(files[i].toString());
    }
    md5 = GetMD5HashOfString(md5);
    return 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();
}

public static String    GetMD5HashOfString  (String str)
    {
        MessageDigest md5 ;        
        StringBuffer  hexString = new StringBuffer();
        try
        {                            
            md5 = MessageDigest.getInstance("md5");         
            md5.reset();
            md5.update(str.getBytes());                       
            byte messageDigest[] = md5.digest();
            for (int i = 0; i < messageDigest.length; i++)
            {
                hexString.append(Integer.toHexString((0xF0 & messageDigest[i])>>4));
                hexString.append(Integer.toHexString (0x0F & messageDigest[i]));
            }
        } 
        catch (Throwable t) {History.Error(t);}      
        return hexString.toString();
    }

关于java - 如何在 Android/Java 中获取文件目录的 MD5 校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280112/

相关文章:

android - 新版本中的 osmdroid tile 加载速度非常慢

visual-studio-2012 - 我想用MD5 Class做一些加密工作,但是找不到?

php - 如何将 md5 字符串转换为普通文本?

java - 从嵌套可选检查中的列表中获取元素

java - Groovy Array.addAll 方法从原始数组中删除元素

java - 如何在按主页按钮后跳过第一个 Activity ?(Android)

android - Samsung Galaxy S4 未检测到 USB 串行 FT232R

java - 多 View 应用程序

java - 用java进行游戏模拟有问题

cryptography - SHA512优于MD5的原因