标签:
#pragma mark --------编码--------------
+ (NSString * )encodeBase64:(NSString * )text
{
NSData * data = [text dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
// 转换到base64
data = [GTMBase64 encodeData:data];
NSString * base64String = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
return base64String;
}
+ (NSString * )decodeBase64:(NSString * )text
{
NSData * data = [text dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
// 转换到普通
data = [GTMBase64 decodeData:data];
NSString * string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
return string;
}
//url编码
+(NSString *)encodeToPercentEscapeString: (NSString *) input
{
NSString *outputStr = (NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@"!*‘();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
if(!outputStr || (NSNull *)outputStr == [NSNull null] || outputStr.length<1)
{
return @"";
}
return outputStr;
}
//md5加密
+(NSString *) md5String:(NSString *)str
{
const char *original_str = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(original_str, (int)strlen(original_str), result);
NSMutableString *hash = [NSMutableString string];
for (int i = 0; i < 16; i++)
[hash appendFormat:@"%02X", result[i]];
return [hash lowercaseString];
}
static inline char hexChar(unsigned char c) {
return c < 10 ? ‘0‘ + c : ‘a‘ + c - 10;
}
static inline void hexString(unsigned char *from, char *to, NSUInteger length) {
for (NSUInteger i = 0; i < length; ++i) {
unsigned char c = from[i];
unsigned char cHigh = c >> 4;
unsigned char cLow = c & 0xf;
to[2 * i] = hexChar(cHigh);
to[2 * i + 1] = hexChar(cLow);
}
to[2 * length] = ‘\0‘;
}
NSString * sha1(const char *string)
{
if(!string)
{
return nil;
}
static const NSUInteger LENGTH = 20;
unsigned char result[LENGTH];
CC_SHA1(string, (CC_LONG)strlen(string), result);
char hexResult[2 * LENGTH + 1];
hexString(result, hexResult, LENGTH);
return [NSString stringWithUTF8String:hexResult];
}
标签:
原文地址:http://www.cnblogs.com/fan-cong/p/5142283.html