IOS计算MD5值

#import <Foundation/Foundation.h>

#import <CommonCrypto/CommonDigest.h>

#define FileHashDefaultChunkSizeForReadingData 1024*8 // 8K

@interface QQYMD5Util : NSObject

//计算NSData MD5

+(NSString*)getMD5WithData:(NSData*)data;


//计算字符串的MD5值,

+(NSString*)getmd5WithString:(NSString*)string;


//计算大文件的MD5

+(NSString*)getFileMD5WithPath:(NSString*)path;

@end



#import "QQYMD5Util.h"


@implementation QQYMD5Util

+ (NSString*)getmd5WithString:(NSString *)string

{

    const char* original_str=[string UTF8String];

    unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16

    CC_MD5(original_str, strlen(original_str), digist);

    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];

    for(int  i =0; i<CC_MD5_DIGEST_LENGTH;i++){

        [outPutStr appendFormat:@"%02x", digist[i]];//小写x表示输出的是小写MD5,大写X表示输出的是大写MD5

    }

    return [outPutStr lowercaseString];

}


+ (NSString*)getMD5WithData:(NSData *)data{

    const char* original_str = (const char *)[data bytes];

    unsigned char digist[CC_MD5_DIGEST_LENGTH]; //CC_MD5_DIGEST_LENGTH = 16

    CC_MD5(original_str, strlen(original_str), digist);

    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];

    for(int  i =0; i<CC_MD5_DIGEST_LENGTH;i++){

        [outPutStr appendFormat:@"%02x",digist[i]];//小写x表示输出的是小写MD5,大写X表示输出的是大写MD5

    }

    

    //也可以定义一个字节数组来接收计算得到的MD5

    //    Byte byte[16];

    //    CC_MD5(original_str, strlen(original_str), byte);

    //    NSMutableString* outPutStr = [NSMutableString stringWithCapacity:10];

    //    for(int  i = 0; i<CC_MD5_DIGEST_LENGTH;i++){

    //        [outPutStr appendFormat:@"%02x",byte[i]];

    //    }

    //    [temp release];

    

    return [outPutStr lowercaseString];

    

}


+(NSString*)getFileMD5WithPath:(NSString*)path

{

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

    

    CC_MD5_CTX hashObject;

    bool hasMoreData = true;

    bool didSucceed;

    

    if (!fileURL) goto done;

    

    // Create and open the read stream

    readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,

                                            (CFURLRef)fileURL);

    if (!readStream) goto done;

    didSucceed = (bool)CFReadStreamOpen(readStream);

    if (!didSucceed) goto done;

    

    // Initialize the hash object

    CC_MD5_Init(&hashObject);

    

    // Make sure chunkSizeForReadingData is valid

    if (!chunkSizeForReadingData) {

        chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;

    }

    

    // Feed the data to the hash object

    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;

}

@end

调用事例:

#include "QQYMD5Util.h"  


NSString* filePath;   

NSString* fileMD5 = [QQYMD5Util getFileMD5WithPath:filePath]; 


参考:http://www.cnblogs.com/ios8/p/ios-md5.html


IOS计算MD5值,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。