码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发之MD5封装及应用

时间:2015-11-19 16:26:28      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

一、MD5的封装

 1 #define CC_MD5_DIGEST_LENGTH    16    
 2 
 3 - (NSString *)toMD5
 4 {
 5     const char* input = [self UTF8String];
 6     unsigned char result[CC_MD5_DIGEST_LENGTH];
 7     CC_MD5(input, (CC_LONG)strlen(input), result);
 8     
 9     NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
10     for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
11         [digest appendFormat:@"%02x", result[i]];
12     }
13     
14     return digest;
15 }

 

二、生成独一无二的图片名称

  1)根据URLString来生成

1 - (NSString*) genImageName:(NSString *)urlString
2 {
3     NSString *imageName = [urlString toMD5];
4     return [imageName stringByAppendingPathExtension:@"png"];
5 }

  2) 根据时间戳来生成(绝对独一无二)

 1 +(NSString *)createCUID
 2 {
 3     NSString *prefix = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
 4     NSString *  result;
 5     CFUUIDRef   uuid;
 6     CFStringRef uuidStr;
 7     uuid = CFUUIDCreate(NULL);
 8     uuidStr = CFUUIDCreateString(NULL, uuid);
 9     result =[NSString stringWithFormat:@"%@-%@",prefix,uuidStr];
10     CFRelease(uuidStr);
11     CFRelease(uuid);
12     return [result toMD5];
13 }
14 
15 //生成图片名,统一用png
16 static inline NSString* genImageName()
17 {
18     NSString *imageName = [NSString createCUID];
19     return [imageName stringByAppendingPathExtension:@"png"];
20 }

 

iOS开发之MD5封装及应用

标签:

原文地址:http://www.cnblogs.com/gaox97329498/p/4977921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!