iOS使用MD5 - 字符串加密至MD5&获取文件MD5
2013-07-11 11:03
686 查看
iOS 字符串加密至MD5
#import <CommonCrypto/CommonDigest.h> + (NSString *) md5:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr, strlen(cStr), result ); return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; }
iPhone处理大文件检测MD5的代码
+(NSString*)fileMD5:(NSString*)path { NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path]; if( handle== nil ) return @"ERROR GETTING FILE MD5"; // file didnt exist CC_MD5_CTX md5; CC_MD5_Init(&md5); BOOL done = NO; while(!done) { NSData* fileData = [handle readDataOfLength: CHUNK_SIZE ]; CC_MD5_Update(&md5, [fileData bytes], [fileData length]); if( [fileData length] == 0 ) done = YES; } unsigned char digest[CC_MD5_DIGEST_LENGTH]; CC_MD5_Final(digest, &md5); NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7], digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]]; return s; }
In my project i need to get the MD_5 hash code of the file in iphone. uptill now i have found the following code to get md_5 of any image/any file.-(NSString*)getMD5FromString:(NSString*)source{constchar*src =[source UTF8String];unsignedchar result[CC_MD5_DIGEST_LENGTH]; CC_MD5(src, strlen(src), result);return[[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]]lowercaseString];} using this code to get the ByteContent of the image and then get the md_5 of that image byte array string UIImage*image =[UIImage imageNamed:@"sf_small.png"];NSData*data =UIImagePNGRepresentation(image);NSString*str =[NSString stringWithFormat:@"%@",data];NSString*temp =[self getMD5FromString:str]; now i am getting a hash code succesfully But when on the web side i get the md_5 hash code of same file then it gives me diferent hash code. in Web side i am using PHP code md5_file(string $filename); this PHP code gives me differnet hash code and iphone code gives me different hash code for same image. Please tell me what can be the problem.. Thanks alot! | |
There are 2 causes. The first is because the raw bytes → string → UTF-8 process corrupted some non-ASCII characters. Note that you can get a pointer to the bytes from an NSData directly: UIImage* image =[UIImage imageNamed:@"sf_small.png"];NSData* data =UIImagePNGRepresentation(image);constvoid* src =[data bytes];NSUInteger len =[data length]; CC_MD5(src, len, result);... The second cause is because of the PNG → raw image → PNG process. There is no guarentee that the same image will compress to the same PNG representation in different libraries, and of course you'll have different MD5. You could just avoid reading the file as image altogether, as it's possible read the file directly as data: NSData* data =[NSData dataWithContentsOfFile:@"sf_small.png"]; const void* src =[data bytes]; NSUInteger len =[data length]; CC_MD5(src, len, result); |
相关文章推荐
- iOS使用MD5 - 字符串加密至MD5&获取文件MD5
- iOS字符串加密至MD5&及获取文件MD5
- 加密:Java利用MessageDigest获取字符串或文件MD5详解
- 如何将iOS字符串使用MD5进行加密
- 使用Des,MD5 加密.解密.字符串.文件
- 详解ios中的SQL数据库文件加密 (使用sqlcipher)
- MD5原理及加密字符串和文件
- iOS 获取文件md5 get file md5
- iPhone开发中 验证”字符串“/“文件”的MD5的加密
- c#大圣之路笔记——MD5实现字符串、文件加密
- iOS上获取文件md5的方法
- MD5原理及加密字符串和文件
- iOS - 获取文件MD5 :
- MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能
- linux md5 加密字符串和文件方法
- linux下使用openssl和md5sum加密文件或者字符串
- 使用commons-codec包加密字符串(MD5,SHA1,BASE64)
- iOS开发之百度免费API使用案例(身份证号获取地址、手机号查归属地、MD5解密等)
- iOS上传视频道优酷视频文件MD5的获取等
- MessageDigest获取字符串或文件MD5详解