iOS - 获取文件MD5 :
2014-04-23 10:32
609 查看
1.首先导入头文件:
2.具体实现方法.
3.实现此方法:
FileMD5HashCreateWithPath((__bridge CFStringRef)([[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.txt.zip"]));
#include <CommonCrypto/CommonDigest.h>
2.具体实现方法.
CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath) { // 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); // Feed the data to the hash object bool hasMoreData = true; while (hasMoreData) { uint8_t buffer[4096]; 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); } NSLog(@"MD5// %@",result); return result; }
3.实现此方法:
FileMD5HashCreateWithPath((__bridge CFStringRef)([[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.txt.zip"]));
相关文章推荐
- iOS 获取文件md5 get file md5
- iOS上获取大文件md5的方法
- iOS上获取文件md5的方法
- iOS上传视频道优酷视频文件MD5的获取等
- iOS上传视频道优酷视频文件MD5的获取等
- iOS使用MD5 - 字符串加密至MD5&获取文件MD5
- iOS上获取文件md5的方法
- iOS字符串加密至MD5&及获取文件MD5
- iOS使用MD5 - 字符串加密至MD5&获取文件MD5
- iOS - 获取文件的MD5
- iOS - 获取音视频文件的Metadata信息
- IOS中获取各种文件的目录路径的方法
- iOS获取plist文件属性值的方法
- 【iOS】获取App的常用文件路径
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- Java利用MessageDigest获取字符串或文件MD5详解
- iOS 项目的文件 获取路径为nil
- iOS 获取文件的目录路径的几种方法
- Spark-MD5获取文件MD5的使用
- iOS拖到工程里的图片 、MP3 文件获取为空