ios和Android的大文件md5校验逻辑

- 原因

保证服务器下发的资源在下载后没有修改,检测完整行

- ios代码 检验文件流

#define FileHashDefaultChunkSizeForReadingData 1024*8
#pragma mark 大文件的md5
-(NSString*)getFileMD5WithPath:(NSString*)path

{
  
  return (__bridge_transfer NSString *)FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
  
}



CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath,size_t chunkSizeForReadingData) {
  
  // Declare needed variables
  
  CFStringRef result = NULL;
  
  CFReadStreamRef readStream = NULL;
  
  // Get the file URL
  
  CFURLRef fileURL =
  
  CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                
                                (CFStringRef)filePath,
                                
                                kCFURLPOSIXPathStyle,
                                
                                (Boolean)false);
  
  if (!fileURL) goto done;
  
  // Create and open the read stream
  
  readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
                                          
                                          (CFURLRef)fileURL);
  
  if (!readStream) goto done;
  
  bool didSucceed = (bool)CFReadStreamOpen(readStream);
  
  if (!didSucceed) goto done;
  
  // Initialize the hash object
  
  CC_MD5_CTX hashObject;
  
  CC_MD5_Init(&hashObject);
  // Make sure chunkSizeForReadingData is valid
  
  if (!chunkSizeForReadingData) {
    
    chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;
    
  }
  
  // Feed the data to the hash object
  
  bool hasMoreData = true;
  
  while (hasMoreData) {
    
    uint8_t buffer[chunkSizeForReadingData];
    
    CFIndex readBytesCount = CFReadStreamRead(readStream,(UInt8 *)buffer,(CFIndex)sizeof(buffer));
    
    if (readBytesCount == -1) break;
    
    if (readBytesCount == 0) {
      
      hasMoreData = false;
      
      continue;
      
    }
    
    CC_MD5_Update(&hashObject,(const void *)buffer,(CC_LONG)readBytesCount);
    
  }
  
  // Check if the read operation succeeded
  
  didSucceed = !hasMoreData;
  
  // Compute the hash digest
  
  unsigned char digest[CC_MD5_DIGEST_LENGTH];
  
  CC_MD5_Final(digest, &hashObject);
  
  // Abort if the read operation failed
  
  if (!didSucceed) goto done;
  // Compute the string result
  
  char hash[2 * sizeof(digest) + 1];
  
  for (size_t i = 0; i < sizeof(digest); ++i) {
    
    snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
    
  }
  
  result = CFStringCreateWithCString(kCFAllocatorDefault,(const char *)hash,kCFStringEncodingUTF8);
  
  
  
done:
  
  if (readStream) {
    
    CFReadStreamClose(readStream);
    
    CFRelease(readStream);
    
  }
  
  if (fileURL) {
    
    CFRelease(fileURL);
    
  }
  return result;
  
}
  

- Android文件流md5的相关代码如下:

 public static String getMD5(String imagePath) throws NoSuchAlgorithmException, IOException {

        InputStream in = new FileInputStream(new File(imagePath));

        StringBuffer md5 = new StringBuffer();
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] dataBytes = new byte[1024];

        int nread = 0;
        while ((nread = in.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }

        byte[] mdbytes = md.digest();

        // convert the byte to hex format
        for (int i = 0; i < mdbytes.length; i++) {
            md5.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        return md5.toString().toLowerCase();
    }

可以得到一样的md5,如果是对字符串操作的话可能得到的结果就不一样了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,376评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,205评论 4 61
  • 辞旧迎新,新的一年又开始了。回顾过去的一年,有许多的哀叹……但也认识了许多新的朋友,也算是收获吧! 祝...
    慧_cbb6阅读 975评论 0 0
  • 孔子曾说过"唯女子和小人难养也",这句话从现代社会的角度看会饱受争议。但从另一个维度看,不得不承认大部分女子和小...
    spiro阅读 1,254评论 0 0
  • 《工作前5年决定你一生的财富》作者:三公子出版社:中国铁道出版社 关于三公子: 这本书关于作者,给三公子的标签是这...
    大林张阅读 4,359评论 5 8