【问题标题】:How to generate an MD5 checksum for a file in Android?如何在 Android 中为文件生成 MD5 校验和?
【发布时间】:2012-10-20 14:29:21
【问题描述】:

在我的应用程序中,我需要为文件生成 MD5 校验和。请问有什么方法可以实现吗?

谢谢。

【问题讨论】:

    标签: android md5 checksum


    【解决方案1】:

    朋友试试下面的代码

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

    【讨论】:

      【解决方案2】:

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

      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

      【讨论】:

      • 请注意,在 Android 4.2.1 之前 MessageDigest.getInstance() 不是线程安全的。检查bug reportfix。所以如果你使用HttpResponseCache,你最好检查一下有没有不同
      • 此方法正在从最终 md5 刺痛中删除初始 0:(。如何克服这个问题?
      • 我来到了 MD5 一个 700MB 的文件,我跳过这个答案的原因应该很明显:)
      • 认真 +1 给 Gerald,将文件转换为字符串听起来是个坏主意 :)
      • 为了记录,MD5 不被认为是加密,而是散列——因此,方法名称应该被称为 getMD5Hash(String s)
      【解决方案3】:

      此代码来自 CMupdater,来自 CyanogenMod 10.2 android ROM。 它将下载的 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);
                  }
              }
          }
      }
      

      【讨论】:

      • 效果很好,而且是 GPL。谢谢你。
      • 不客气。是的,GPL。它传播到整个应用程序。
      • 很好的参考!还是很有用的!
      • CyanogenMod 岩石.. ;)
      • 因为 GPL 而无用
      【解决方案4】:

      我有同样的任务,这段代码运行良好:

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

      【讨论】:

        【解决方案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();
        }
        

        【讨论】:

          【解决方案6】:

          这个方法对我有用,在一个 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);
                      }
                  }
              }   
          

          【讨论】:

            【解决方案7】:

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

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

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

            【讨论】:

            • 你确定这个二进制文件存在于每个 Android 安装中吗?在我的设备上,它被称为 md5sum。
            • @bk138 不再。我确实认为在执行任何其他提到的方法之前检查它是否是值得的,因为这个二进制文件的速度比任何 Java 实现都要好。
            • 效果很好,但我认为不适用于 sha256。至少我在我的 AVD 上找不到任何 shasum、sha256sum、openssl dgst -sha256、...。
            • 10 个左右相同答案的有趣替代方案。
            【解决方案8】:

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

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

            【讨论】:

            • 这应该是公认的答案。原因正如@gak 在接受答案的评论中所说。
            • 请给 bufferSize 一个值?
            • @Renaud 如果你真的需要优化它,你可能会做这样的事情 stackoverflow.com/questions/10143731/… 否则使用 32k
            【解决方案9】:

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

            /**
             * 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(哈希)

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

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

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

            【讨论】:

              【解决方案10】:

              如果您使用的是 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 来做类似的事情。

              【讨论】:

              • 这种方式更简单:val md5_as_hex_string = Okio.buffer(Okio.source(file).readByteString().md5().hex()
              【解决方案11】:

              Kotlin 版本:

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

              【讨论】:

              • 为了防止异常短路stream.close(),您可以将stream.use包裹在流读取循环周围并删除stream.close()
              【解决方案12】:

              我在 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】:

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

                【讨论】:

                  【解决方案14】:
                   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));
                  }
                  

                  【讨论】:

                    【解决方案15】:
                        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) }
                        }
                    

                    【讨论】:

                      【解决方案16】:

                      使用 OKio,它是一条线:

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

                      【讨论】:

                        【解决方案17】:

                        在科特林中

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

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 2021-12-19
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多