40位UUID, 及一个32位的不知是啥

因为项目需要, 所以需要使用到一个唯一识别一个硬件的方法。

当然我们知道,目前uuid已经被苹果给禁用。


即如下方法取uuid串。会取得一个40位的串。

    UIDevice *myDevice = [UIDevicecurrentDevice];

    NSString *deviceUDID = [myDevice uniqueIdentifier];

但因为uuid已被禁用,所以我们只得想其它办法,结果在网上搜到下面这个方法,经验证, 确实可以生成一个唯一的串,但是是32位的,不知是什么东西。但看起来似乎确实是唯一的。 有知道的, 还请留言分享一下,谢谢!

  1. - (NSString *)createUUID  
  2. {  
  3.   // Create universally unique identifier (object)  
  4.   CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);  
  5.    
  6.   // Get the string representation of CFUUID object.  
  7.   NSString *uuidStr = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);  
  8.    
  9.   // If needed, here is how to get a representation in bytes, returned as a structure  
  10.   // typedef struct {  
  11.   //   UInt8 byte0;  
  12.   //   UInt8 byte1;  
  13.   //   ...  
  14.   //   UInt8 byte15;  
  15.   // } CFUUIDBytes;  
  16.   CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);  
  17.    
  18.   CFRelease(uuidObject);  
  19.    
  20.   return uuidStr;  
  21. }  

下面是我iphone4的结果

 createUUID=A450EA27-xxxx-xxxx-xxxx-02B752DB82FA,再次运行。发现它是F783E91C-A0DD-4B06-xxxx-CA7967B049EE, 所以可以认为它确实不是唯一的。

 deviceUDID=c96f6d0f9321df1cce375bcf7d6e50fe(末尾去掉8位,呵呵,隐私)


后来在网上搜了一遍, 发现有人说上面这个方法生成的32位串,每次运行都会不一样,经证实,确实是这样子的。
转一下那篇文章在下面:

1.通过调用CFFUUIDCreate函数来生成机器唯一标识符,但每次调用以下函数返回的字符串都不一样,所以第一次调用后需把该字符串存储起来。这是官方API的建议方法

- (NSString *) uniqueString

{

CFUUIDRef unique = CFUUIDCreate(kCFAllocatorDefault);

NSString *result = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, unique) autorelease];

CFRelease(unique);

return result;

}


2.由于机器的mac地址都不一样,所以可通过获取mac地址后再进行md5加密处理后返回的字符串作为唯一标识码, Heqin:原理就是通过Mac地址,然后再用md5值处理一下,最终成为唯一标识码。个人认为,因为Mac地址本身的唯一性,其实也可以做为唯一识别的身份。(除非有识备没有上网模块,但好像没有这样的设备), 注意:原贴中没有包含下面这几个 include会导致编译失败,需要加上。

#include <sys/socket.h> 

#include <sys/sysctl.h>

#include <net/if.h>

#include <net/if_dl.h>

- (NSString *) macaddress{

    

    int                 mib[6];

    size_t              len;

    char                *buf;

    unsigned char       *ptr;

    struct if_msghdr    *ifm;

    struct sockaddr_dl  *sdl;

    

    mib[0] = CTL_NET;

    mib[1] = AF_ROUTE;

    mib[2] = 0;

    mib[3] = AF_LINK;

    mib[4] = NET_RT_IFLIST;

    

    if ((mib[5] = if_nametoindex("en0")) == 0) {

        printf("Error: if_nametoindex error\n");

        return NULL;

    }

    

    if (sysctl(mib, 6NULL, &len, NULL0) < 0) {

        printf("Error: sysctl, take 1\n");

        return NULL;

    }

    

    if ((buf = malloc(len)) == NULL) {

        printf("Could not allocate memory. error!\n");

        return NULL;

    }

    

    if (sysctl(mib, 6, buf, &len, NULL0) < 0) {

        printf("Error: sysctl, take 2");

        return NULL;

    }

    

    ifm = (struct if_msghdr *)buf;

    sdl = (struct sockaddr_dl *)(ifm + 1);

    ptr = (unsigned char *)LLADDR(sdl);

    NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X"

                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];

    free(buf);

    

    return outstring;

}


- (NSString *) uniqueGlobalDeviceIdentifier{

    NSString *macaddress = [[UIDevice currentDevicemacaddress];

    NSString *uniqueIdentifier = [macaddress stringFromMD5];

    

    return uniqueIdentifier;

}


NSString类别 Heqin:下面这个方法需要做如下引用,原贴没有加,会编译通不过:

#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5Addition)


- (NSString *) stringFromMD5{

    

    if(self == nil || [self length] == 0)

        return nil;

    

    const char *value = [self UTF8String];

    

    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];

    CC_MD5(value, strlen(value), outputBuffer);

    

    NSMutableString *outputString = [[NSMutableString allocinitWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){

        [outputString appendFormat:@"%02x",outputBuffer[count]];

    }

    

    return [outputString autorelease];

转http://blog.163.com/ray_jun/blog/static/1670536422011102744836300/ 

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值