70

在我的应用程序中,我需要为文件生成 MD5 校验和。您能否告诉我是否有任何方法可以实现这一目标?

谢谢你。

4

17 回答 17

152

此代码来自 CyanogenMod 10.2 android ROM 的 CMupdater。它将下载的 ROM 测试到更新程序应用程序中。

代码:https ://github.com/CyanogenMod/android_packages_apps_CMUpdater/blob/cm-10.2/src/com/cyanogenmod/updater/utils/MD5.java

它就像一个魅力:

/*
 * Copyright (C) 2012 The CyanogenMod Project
 *
 * * Licensed under the GNU GPLv2 license
 *
 * The text of the license can be found in the LICENSE file
 * or at https://www.gnu.org/licenses/gpl-2.0.txt
 */

package com.cyanogenmod.updater.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    private static final String TAG = "MD5";

    public static boolean checkMD5(String md5, File updateFile) {
        if (TextUtils.isEmpty(md5) || updateFile == null) {
            Log.e(TAG, "MD5 string empty or updateFile null");
            return false;
        }

        String calculatedDigest = calculateMD5(updateFile);
        if (calculatedDigest == null) {
            Log.e(TAG, "calculatedDigest null");
            return false;
        }

        Log.v(TAG, "Calculated digest: " + calculatedDigest);
        Log.v(TAG, "Provided digest: " + md5);

        return calculatedDigest.equalsIgnoreCase(md5);
    }

    public static String calculateMD5(File updateFile) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "Exception while getting digest", e);
            return null;
        }

        InputStream is;
        try {
            is = new FileInputStream(updateFile);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Exception while getting FileInputStream", e);
            return null;
        }

        byte[] buffer = new byte[8192];
        int read;
        try {
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            // Fill to 32 chars
            output = String.format("%32s", output).replace(' ', '0');
            return output;
        } catch (IOException e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "Exception on closing MD5 input stream", e);
            }
        }
    }
}
于 2013-02-17T15:04:15.283 回答
43

将文件内容转换为字符串并使用以下方法:

public static String getMD5EncryptedString(String encTarget){
        MessageDigest mdEnc = null;
        try {
            mdEnc = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Exception while encrypting to md5");
            e.printStackTrace();
        } // Encryption algorithm
        mdEnc.update(encTarget.getBytes(), 0, encTarget.length());
        String md5 = new BigInteger(1, mdEnc.digest()).toString(16);
        while ( md5.length() < 32 ) {
            md5 = "0"+md5;
        }
        return md5;
    }

请注意,这种简单的方法适用于小字符串,但对于大文件效率不高。对于后者,请参阅dentex 的回答

于 2012-10-31T06:59:19.693 回答
11

我有同样的任务,这段代码效果很好:

public static String fileToMD5(String filePath) {
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(filePath);
        byte[] buffer = new byte[1024];
        MessageDigest digest = MessageDigest.getInstance("MD5");
        int numRead = 0;
        while (numRead != -1) {
            numRead = inputStream.read(buffer);
            if (numRead > 0)
                digest.update(buffer, 0, numRead);
        }
        byte [] md5Bytes = digest.digest();
        return convertHashToString(md5Bytes);
    } catch (Exception e) {
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) { }
        }
    }
}

private static String convertHashToString(byte[] md5Bytes) {
    String returnVal = "";
    for (int i = 0; i < md5Bytes.length; i++) {
        returnVal += Integer.toString(( md5Bytes[i] & 0xff ) + 0x100, 16).substring(1);
    }
    return returnVal.toUpperCase();
}
于 2013-06-05T11:26:56.400 回答
6
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();
}
于 2014-05-23T20:36:34.717 回答
6

如果您使用的是 Okio(目前大多数应用程序都在使用 Okio,直接或间接使用 OkHttp 或 Retrofit),您还可以执行以下操作:

return File(path).source().buffer().use { source ->
   HashingSink.md5(blackholeSink()).use { sink ->
     source.readAll(sink)
     sink.hash.hex()
   }
}

这不必在内存中缓冲整个文件(HashingSink每次write调用都会更新 md5sum,然后调用到blackholeSink(),这对字节没有任何作用)。您也可以使用HashingSource来做类似的事情。

于 2020-04-14T20:52:26.607 回答
5

如果你需要计算大文件的 MD5,你可能喜欢使用这个:

进口:

import java.security.MessageDigest;

方法:

 private byte[] calculateMD5ofFile(String location) throws IOException, NoSuchAlgorithmException {
        FileInputStream fs= new FileInputStream(location);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] buffer=new byte[bufferSize];
        int bytes=0;
        do{
            bytes=fs.read(buffer,0,bufferSize);
            if(bytes>0)
                md.update(buffer,0,bytes);

        }while(bytes>0);
        byte[] Md5Sum = md.digest();
        return Md5Sum;
    }

参考: https ://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html


将字节数组转换为十六进制。用这个

public static String ByteArraytoHexString(byte[] bytes) {
    StringBuilder hexString = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
        String hex = Integer.toHexString(bytes[i] & 0xFF);
        if (hex.length() == 1) {
            hexString.append('0');
        }
        hexString.append(hex);
    }
    return hexString.toString();
}

参考在Java中,如何将字节数组转换为十六进制数字字符串,同时保持前导零?

于 2016-09-16T08:58:57.163 回答
4

这是干净的小 kotlin 扩展功能。也适用于大文件。

fun File.md5(): String {
    val md = MessageDigest.getInstance("MD5")
    return this.inputStream().use { fis ->
        val buffer = ByteArray(8192)
        generateSequence {
            when (val bytesRead = fis.read(buffer)) {
                -1 -> null
                else -> bytesRead
            }
        }.forEach { bytesRead -> md.update(buffer, 0, bytesRead) }
        md.digest().joinToString("") { "%02x".format(it) }
    }
}

以及与之配套的单元测试:

@Test
fun `computes md5 checksum correctly`() {
    val file = File.createTempFile("test-", ".tmp")
    // did md5 on unix machine to comfirm -- put a literal LF at end to compare
    val content = "This is the content of a file." + 0x0a.toChar()
    file.writer().use { w -> w.write(content) }
    assertEquals("a149f5161e873921d84636b2a1b3aad2", file.md5())
}
于 2020-07-18T00:42:59.520 回答
3

哥们试试下面的代码

MessageDigest md = MessageDigest.getInstance("MD5");
InputStream is = new FileInputStream("file.txt");
try {
      is = new DigestInputStream(is, md);
      // read stream to EOF as normal...
    }
finally {
      is.close();
   }
byte[] digest = md.digest();
于 2012-10-31T06:58:12.683 回答
3

我发现以下工作非常好:

Process process = Runtime.getRuntime().exec("md5 "+fileLocation);
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
String result = inputStream.readLine().split(" ")[0];

这调用了内置md5命令。该变量fileLocation将被设置为文件的位置。当然,我确实建议在此处构建一些检查以检查文件是否存在。

于 2016-02-04T09:44:10.147 回答
2

这个方法对我有用,在一个 131MB 的 zip 文件上。由 AccuHash ( http://www.accuhash.com )在同一文件上计算的 MD5 计算匹配

public static String calculateMD5(File updateFile) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            Log.e("calculateMD5", "Exception while getting Digest", e);
            return null;
        }

        InputStream is;
        try {
            is = new FileInputStream(updateFile);
        } catch (FileNotFoundException e) {
            Log.e("calculateMD5", "Exception while getting FileInputStream", e);
            return null;
        }

        byte[] buffer = new byte[8192];
        int read;
        try {
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            // Fill to 32 chars
            output = String.format("%32s", output).replace(' ', '0');
            return output;
        } catch (IOException e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.e("calculateMD5", "Exception on closing MD5 input stream", e);
            }
        }
    }   
于 2015-10-22T16:28:07.580 回答
2

科特林版本:



fun File.getMD5Hash(path: String): ByteArray {
    val md = MessageDigest.getInstance("MD5")
    val stream: InputStream
    stream = FileInputStream(this)

    val buffer = ByteArray(8192)
    var read: Int
    while (stream.read(buffer).also { read = it } > 0) {
        md.update(buffer, 0, read)
    }
    stream.close()
    return md.digest()
}
于 2020-05-31T11:09:54.297 回答
1
 public static String md5(String data) throws NoSuchAlgorithmException {
    // Get the algorithm:
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    // Calculate Message Digest as bytes:
    byte[] digest = md5.digest(data.getBytes(StandardCharsets.UTF_8));
    // Convert to 32-char long String:
    return String.format("%032x", new BigInteger(1, digest));
}
于 2020-11-08T09:59:27.877 回答
1
    fun md5(file: File): String {
        val digest = MessageDigest.getInstance(MD5_ALGORITHM)
        file.inputStream().buffered(BUFFER_SIZE).use { it.iterator().forEach(digest::update) }
        return digest.digest().joinToString("") { "%02x".format(it) }
    }
于 2021-04-10T18:13:48.930 回答
0

这是我完整的工作代码。我需要使用校验和查找重复文件。

/**
 * this method is used for create check Sum further process...
 *
 * @param models    path of image.
 * @param asyncTask asyncTask of activity
 * @return return array of all files check sum.
 * <p>
 * before put BufferedInputStream
 * with BufferedInputStream (buffer 8192) with Logs
 * with BufferedInputStream (buffer 16384) with Logs
 * with BufferedInputStream (buffer 4194304) with Logs
 * with BufferedInputStream (buffer 32768) with Logs
 * with BufferedInputStream (buffer 32768) without Logs(MD5)
 * with BufferedInputStream (buffer 32768) without Logs (SHA-256)
 */
public static ArrayList<FileModel> generateCheckSum(ScanningListener scanningListener, ArrayList<FileModel> lstAllFile, AsyncTask asyncTask) {
    FileInputStream fis;
    MessageDigest md;
    byte[] buffer;
    int numOfBytesRead;
    byte[] hash;

    long startTime = System.currentTimeMillis();
    for (FileModel s : lstAllFile) {

        if (scanningListener != null)
            scanningListener.onGoingProgress(lstAllFile.size(),lstAllFile.indexOf(s));
        try {
            if (asyncTask.isCancelled()) {
                break;
            }

            fis = new FileInputStream(s.getFilePath());
            md = MessageDigest.getInstance("MD5");
            buffer = new byte[16384];//(1024*2048)


            while ((numOfBytesRead = fis.read(buffer)) > 0) {
                md.update(buffer, 0, numOfBytesRead);
            }

            hash = md.digest();
            s.setChecksum(convertHashToString(hash));
            CustomLog.error("path", String.valueOf(s.getFilePath()));
        } catch (IOException ex) {
            CustomLog.error("IOException", String.valueOf(ex));
        } catch (NoSuchAlgorithmException ex) {
            CustomLog.error("NoSuchAlgorithmException ", String.valueOf(ex));
        }
    }
    long endTime = System.currentTimeMillis();

    long totalTime = endTime - startTime;
    CustomLog.error("Total Time : ", TimeUtils.getDateIn24HrsFormatInUTC(totalTime));
    return lstAllFile;
}

转换哈希到字符串(哈希)

/**
 * this method is help for convert hash value into string file and return hash code.
 *
 * @param hash byte array.
 * @return return string of hash code
 */
private static String convertHashToString(byte[] hash) {
    StringBuilder returnVal = new StringBuilder();
    for (byte md5Byte : hash) {
        returnVal.append(Integer.toString((md5Byte & 0xff) + 0x100, 16).substring(1));
    }
    return returnVal.toString();
}

此方法将为您提供所有给定文件的哈希图。

我尝试了许多不同类型的缓冲区大小以及 MD5 和 SHA-1,您可以在评论部分看到

于 2020-02-24T13:14:49.200 回答
0

我在 Kotlin 中使用这两个扩展:

fun File.calcHash(algorithm: String = "MD5", bufferSize: Int = 1024): ByteArray {
    this.inputStream().use { input ->
        val buffer = ByteArray(bufferSize)
        val digest = MessageDigest.getInstance(algorithm)

        read@ while (true) {
            when (val bytesRead = input.read(buffer)) {
                -1 -> break@read
                else -> digest.update(buffer, 0, bytesRead)
            }
        }

        return digest.digest()
    }
}

fun ByteArray.toHexString(): String {
    return this.fold(StringBuilder()) { result, b -> result.append(String.format("%02X", b)) }.toString()
}
于 2020-06-28T15:54:45.040 回答
0

使用 OKio,它是单行的:

val md5_as_hex_string = Okio.buffer(Okio.source(file).readByteString().md5().hex()
于 2021-05-19T20:43:04.583 回答
0

在科特林

private fun calculateMd5(file: File): String {
    val digest = MessageDigest.getInstance("MD5")
    file.inputStream().buffered(8192).use { it.iterator().forEach(digest::update) }
    return digest.digest().joinToString("") { "%02x".format(it) }.toUpperCase()
}
于 2021-12-13T20:55:59.647 回答