|
//
// NSData+EasyExtend.m
//
// Created by EasyIOS on 14-4-10.
// Copyright (c) 2014年 zhuchao. All rights reserved.
//
#import "NSData+EasyExtend.h"
@implementation NSData (EasyExtend)
- (NSData *)MD5
{
unsigned char md5Result[CC_MD5_DIGEST_LENGTH + 1];
CC_LONG md5Length = (CC_LONG)[self length];
CC_MD5( [self bytes], md5Length, md5Result );
NSMutableData * retData = [[NSMutableData alloc] init];
if ( nil == retData )
return nil;
[retData appendBytes:md5Result length:CC_MD5_DIGEST_LENGTH];
return retData;
}
- (NSString *)MD5String
{
NSData * value = [self MD5];
if ( value )
{
char tmp[16];
unsigned char * hex = (unsigned char *)malloc( 2048 + 1 );
unsigned char * bytes = (unsigned char *)[value bytes];
unsigned long length = [value length];
hex[0] = '\0';
for ( unsigned long i = 0; i < length; ++i )
{
sprintf( tmp, "%02X", bytes[i] );
strcat( (char *)hex, tmp );
}
NSString * result = [NSString stringWithUTF8String:(const char *)hex];
free( hex );
return result;
}
else
{
return nil;
}
}
+ (NSData *)fromResource:(NSString *)resName
{
NSString * extension = [resName pathExtension];
NSString * fullName = [resName substringToIndex:(resName.length - extension.length - 1)];
NSString * path = [[NSBundle mainBundle] pathForResource:fullName ofType:extension];
return [NSData dataWithContentsOfFile:path];
}
- (NSString*)dataToString
{
return [self base64Encoding];
}
@end
|