XYDeviceInfoUUID.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // XYDeviceInfoUUID.m
  3. // XYUUID
  4. //
  5. // Created by steve on 2016/7/21.
  6. // Copyright © 2020 guojunliu.github.io. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "XYDeviceInfoUUID.h"
  10. #import <sys/sysctl.h>
  11. #import <CommonCrypto/CommonDigest.h>
  12. #import <CoreTelephony/CTCarrier.h>
  13. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  14. @implementation XYDeviceInfoUUID
  15. static NSString *systemBootTime(){
  16. struct timeval boottime;
  17. size_t len = sizeof(boottime);
  18. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  19. if( sysctl(mib, 2, &boottime, &len, NULL, 0) < 0 )
  20. {
  21. return @"";
  22. }
  23. time_t bsec = boottime.tv_sec / 10000;
  24. // time_t bsec = 1476249507 / 10000; // 2016/10/11 13:18:27
  25. // time_t bsec = 1476249507 / 10000; // 2015/10/11 13:18:27
  26. NSString *bootTime = [NSString stringWithFormat:@"%ld",bsec];
  27. return bootTime;
  28. }
  29. static NSString *countryCode() {
  30. NSLocale *locale = [NSLocale currentLocale];
  31. NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
  32. return countryCode;
  33. }
  34. static NSString *language() {
  35. NSString *language;
  36. NSLocale *locale = [NSLocale currentLocale];
  37. if ([[NSLocale preferredLanguages] count] > 0) {
  38. language = [[NSLocale preferredLanguages]objectAtIndex:0];
  39. } else {
  40. language = [locale objectForKey:NSLocaleLanguageCode];
  41. }
  42. return language;
  43. }
  44. static NSString *systemVersion() {
  45. return [[UIDevice currentDevice] systemVersion];
  46. }
  47. static NSString *deviceName(){
  48. return [[UIDevice currentDevice] name];
  49. }
  50. static const char *SIDFAModel = "hw.model";
  51. static const char *SIDFAMachine = "hw.machine";
  52. static NSString *getSystemHardwareByName(const char *typeSpecifier) {
  53. size_t size;
  54. sysctlbyname(typeSpecifier, NULL, &size, NULL, 0);
  55. char *answer = malloc(size);
  56. sysctlbyname(typeSpecifier, answer, &size, NULL, 0);
  57. NSString *results = [NSString stringWithUTF8String:answer];
  58. free(answer);
  59. return results;
  60. }
  61. static NSUInteger getSysInfo(uint typeSpecifier) {
  62. size_t size = sizeof(int);
  63. int results;
  64. int mib[2] = {CTL_HW, typeSpecifier};
  65. sysctl(mib, 2, &results, &size, NULL, 0);
  66. return (NSUInteger) results;
  67. }
  68. static NSString *carrierInfo() {
  69. NSMutableString* cInfo = [NSMutableString string];
  70. CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
  71. CTCarrier *carrier = [networkInfo subscriberCellularProvider];
  72. NSString *carrierName = [carrier carrierName];
  73. if (carrierName != nil){
  74. [cInfo appendString:carrierName];
  75. }
  76. NSString *mcc = [carrier mobileCountryCode];
  77. if (mcc != nil){
  78. [cInfo appendString:mcc];
  79. }
  80. NSString *mnc = [carrier mobileNetworkCode];
  81. if (mnc != nil){
  82. [cInfo appendString:mnc];
  83. }
  84. return cInfo;
  85. }
  86. static NSString *systemHardwareInfo(){
  87. NSString *model = getSystemHardwareByName(SIDFAModel);
  88. NSString *machine = getSystemHardwareByName(SIDFAMachine);
  89. NSString *carInfo = carrierInfo();
  90. NSUInteger totalMemory = getSysInfo(HW_PHYSMEM);
  91. return [NSString stringWithFormat:@"%@,%@,%@,%td",model,machine,carInfo,totalMemory];
  92. }
  93. static NSString *systemFileTime(){
  94. NSFileManager *file = [NSFileManager defaultManager];
  95. NSDictionary *dic= [file attributesOfItemAtPath:@"System/Library/CoreServices" error:nil];
  96. return [NSString stringWithFormat:@"%@,%@",[dic objectForKey:NSFileCreationDate],[dic objectForKey:NSFileModificationDate]];
  97. }
  98. static NSString *disk(){
  99. NSDictionary *fattributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
  100. NSString *diskSize = [[fattributes objectForKey:NSFileSystemSize] stringValue];
  101. return diskSize;
  102. }
  103. static void MD5_16(NSString *source, unsigned char *ret){
  104. const char* str = [source UTF8String];
  105. unsigned char result[CC_MD5_DIGEST_LENGTH];
  106. CC_MD5(str, (CC_LONG)strlen(str), result);
  107. for(int i = 4; i < CC_MD5_DIGEST_LENGTH - 4; i++) {
  108. ret[i-4] = result[i];
  109. }
  110. }
  111. static NSString *combineTwoFingerPrint(unsigned char *fp1,unsigned char *fp2){
  112. NSMutableString *hash = [NSMutableString stringWithCapacity:36];
  113. for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i+=1)
  114. {
  115. if (i==4 || i== 6 || i==8 || i==10)
  116. [hash appendString:@"-"];
  117. if (i < 8) {
  118. [hash appendFormat:@"%02X",fp1[i]];
  119. }else{
  120. [hash appendFormat:@"%02X",fp2[i-8]];
  121. }
  122. }
  123. return hash;
  124. }
  125. + (NSString *)createDeviceInfoUUID {
  126. NSString *sysBootTime = systemBootTime();
  127. NSString *countryC= countryCode();
  128. NSString *languge = language();
  129. NSString *deviceN = deviceName();
  130. NSString *sysVer = systemVersion();
  131. NSString *systemHardware = systemHardwareInfo();
  132. NSString *systemFT = systemFileTime();
  133. NSString *diskS = disk();
  134. NSString *fingerPrintUnstablePart = [NSString stringWithFormat:@"%@,%@,%@,%@", sysBootTime, countryC, languge, deviceN];
  135. NSString *fingerPrintStablePart = [NSString stringWithFormat:@"%@,%@,%@,%@", sysVer, systemHardware, systemFT, diskS];
  136. unsigned char fingerPrintUnstablePartMD5[CC_MD5_DIGEST_LENGTH/2];
  137. MD5_16(fingerPrintUnstablePart,fingerPrintUnstablePartMD5);
  138. unsigned char fingerPrintStablePartMD5[CC_MD5_DIGEST_LENGTH/2];
  139. MD5_16(fingerPrintStablePart,fingerPrintStablePartMD5);
  140. NSString *simulateIDFA = combineTwoFingerPrint(fingerPrintStablePartMD5,fingerPrintUnstablePartMD5);
  141. return simulateIDFA;
  142. }
  143. @end