How can I make ios UDID shorter but still unique enlough

吃可爱长大的小学妹 提交于 2019-12-08 06:07:12

问题


I want to make the UDID of a iOS device to less than or equal to 15 characters.

I am porting a Symbian based client-server software to iOS. The server side uses IMEI (15 digits)to id a client phone. Now on iOS, UDID is too long(40 hex digits). As I want to minimize the change of server program or DB, I need to store the UDID in a varchar(15).

So it's there any way to make the UDID shorter but still unique enlough. It could be much better if I can also get the UDID from the shorted string.


回答1:


You could convert the hex digits into 20 Latin-1 characters (≤0xff) or 10 Unicode BMP characters (≤0xffff).

If that varchar(15) can accept Unicode BMP characters, then we are done.

Otherwise, you could chop the last 5 characters from that 20 Latin-1 characters. The UDID is in fact a SHA-1 hash of some device-unique values, which can be considered quite random and the digits are uniformly distributed. Therefore, with 15 Latin-1 characters the reduced UDID should be able to represent 25615 ~ 1036 devices, which is much more than enough.

In fact, even if you just take 15 hex characters from the UDID it could already represent ~ 1018 devices.

Note that the last 2 methods are lossy, i.e. there is no way you could get the complete UDID from the 15 characters.




回答2:


I just wrote this gist -> https://gist.github.com/3996097

What do you think about a CFUUID + sha1 + substring + random uppercase ?

//Get a random hash (Generated from CFUUID+sha1)
NSString *hash = [NSString sha1:[NSString getUUID]];

//Shorten the sha1
NSString *short_random_id = [hash substringFromIndex:[hash length]-10];

//Random uppercase / lowercase the id
NSMutableString *random_id_final = [NSMutableString string];
for (NSUInteger i = 0; i < [short_random_id length]; i++)
{
    NSString *substring = [short_random_id substringWithRange:NSMakeRange(i, 1)];
    [random_id_final appendString:(rand() % 2) ? [substring lowercaseString] : [substring uppercaseString]];
}


来源:https://stackoverflow.com/questions/5535252/how-can-i-make-ios-udid-shorter-but-still-unique-enlough

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!