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

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

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


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

UIDevice *myDevice = [UIDevicecurrentDevice];

NSString *deviceUDID = [myDevice uniqueIdentifier];

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

  1. -(NSString*)createUUID
  2. {
  3. //Createuniversallyuniqueidentifier(object)
  4. CFUUIDRefuuidObject=CFUUIDCreate(kCFAllocatorDefault);
  5. //GetthestringrepresentationofCFUUIDobject.
  6. NSString*uuidStr=(NSString*)CFUUIDCreateString(kCFAllocatorDefault,uuidObject);
  7. //Ifneeded,hereishowtogetarepresentationinbytes,returnedasastructure
  8. //typedefstruct{
  9. //UInt8byte0;
  10. //UInt8byte1;
  11. //...
  12. //UInt8byte15;
  13. //}CFUUIDBytes;
  14. CFUUIDBytesbytes=CFUUIDGetUUIDBytes(uuidObject);
  15. CFRelease(uuidObject);
  16. returnuuidStr;
  17. }

下面是我iphone4的结果

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

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


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

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

- (NSString*) uniqueString

{

CFUUIDRefunique =CFUUIDCreate(kCFAllocatorDefault);

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

CFRelease(unique);

returnresult;

}


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;

unsignedchar *ptr;

structif_msghdr *ifm;

structsockaddr_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");

returnNULL;

}

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

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

returnNULL;

}

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

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

returnNULL;

}

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

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

returnNULL;

}

ifm = (structif_msghdr*)buf;

sdl = (structsockaddr_dl*)(ifm +1);

ptr = (unsignedchar*)LLADDR(sdl);

NSString*outstring = [NSStringstringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",

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

free(buf);

returnoutstring;

}


- (NSString*) uniqueGlobalDeviceIdentifier{

NSString*macaddress = [[UIDevicecurrentDevice]macaddress];

NSString*uniqueIdentifier = [macaddressstringFromMD5];

returnuniqueIdentifier;

}


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

#import <CommonCrypto/CommonDigest.h>

@implementationNSString(MD5Addition)


- (NSString*) stringFromMD5{

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

returnnil;

constchar*value = [selfUTF8String];

unsignedcharoutputBuffer[CC_MD5_DIGEST_LENGTH];

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

NSMutableString*outputString = [[NSMutableStringalloc]initWithCapacity:CC_MD5_DIGEST_LENGTH*2];

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

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

}

return[outputStringautorelease];

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

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值