如何在Android中为文件生成MD5校验和?

时间:2012-10-31 06:55:55

标签: android md5 checksum

在我的应用中,我需要为文件生成MD5校验和。你能否告诉我是否有办法实现这一目标?

谢谢。

16 个答案:

答案 0 :(得分:130)

此代码来自CMupdater,来自CyanogenMod 10.2安卓ROM。 它将下载的ROM测试到更新程序App。

代码: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);
            }
        }
    }
}

答案 1 :(得分:41)

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

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's answer

答案 2 :(得分:7)

我有同样的任务,这段代码非常出色:

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();
}

答案 3 :(得分:5)

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();
}

答案 4 :(得分:5)

如果您使用的是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来执行类似的操作。

答案 5 :(得分:2)

哥们尝试以下代码

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();

答案 6 :(得分:2)

此方法适用于我,使用131MB的zip文件。 MD5计算了AccuHash(http://www.accuhash.com

在同一文件上计算的匹配项
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);
            }
        }
    }   

答案 7 :(得分:2)

我发现以下内容非常有效:

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

这会调用内置的md5命令。变量fileLocation将设置为文件的位置。当然我建议在这附近构建一些检查来检查文件是否存在。

答案 8 :(得分: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()
}

答案 9 :(得分:1)

这里是干净的小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())
}

答案 10 :(得分:0)

如果你需要计算大文件的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;
    }

Refrence: 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();
}

参考In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?

答案 11 :(得分: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;
}

convertHashToString(hash)

/**
 * 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,您可以在评论部分看到

答案 12 :(得分: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()
}

答案 13 :(得分:0)

 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));
}

答案 14 :(得分:0)

    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) }
    }

答案 15 :(得分:0)

使用 OKio,它是单线的:

val md5_as_hex_string = Okio.buffer(Okio.source(file).readByteString().md5().hex()