1.随机生成一串字符.可以用来当做文件名或其他:
+ (NSString*) createUuidString { // create a new UUID which you own CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); // create a new CFStringRef (toll-free bridged to NSString) // that you own NSString *uuidString = (__bridge NSString*)CFUUIDCreateString(kCFAllocatorDefault, uuid); // release the UUID CFRelease(uuid); // transfer ownership of the string // to the autorelease pool return uuidString; }
2.根据需要,可以对生成的UUID进行MD5或者SHA1加密(感觉有点脑残,注意可能需要 字符编码转化UTF8):
- (NSString*)md5Hash { unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5([self bytes], [self length], 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] ]; }
- (NSString*)sha1Hash { unsigned char result[CC_SHA1_DIGEST_LENGTH]; CC_SHA1([self bytes], [self length], result); return [NSString stringWithFormat: @"%02x%02x%02x%02x%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], result[16], result[17], result[18], result[19] ]; }