forked from rakeen-zz/CAID-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAIDDGenerator.m
403 lines (361 loc) · 13.7 KB
/
CAIDDGenerator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
// CAIDDGenerator.m
// Params
//
// Created by jesse on 2020/7/17.
// Copyright © 2020 none. All rights reserved.
//
#import "CAIDDGenerator.h"
#import <sys/sysctl.h>
#import <UIKit/UIDevice.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import <CommonCrypto/CommonDigest.h>
@implementation CAIDDGenerator
#pragma mark utils
//object to json string
+ (NSString *)objToString:(id)obj {
NSError *error = nil;
NSData *data = nil;
NSString *ret = nil;
@try {
data = [NSJSONSerialization dataWithJSONObject:[self convertToJson:obj]
options:(NSJSONWritingOptions)0
error:&error];
}
@catch (NSException *exception) {
NSLog(@"exception jso:%@", exception);
}
if (error) {
NSLog(@"error jso:%@", error);
} else {
ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
return ret;
}
//convert foundation to json type
+ (id)convertToJson:(id)obj {
if ([obj isKindOfClass:NSString.class] || [obj isKindOfClass:NSNumber.class] || [obj isKindOfClass:NSNull.class]) {
return obj;
}
if ([obj isKindOfClass:NSArray.class]) {
NSMutableArray *a = [NSMutableArray array];
for (id i in obj) {
[a addObject:[self convertToJson:i]];
}
return [NSArray arrayWithArray:a];
}
if ([obj isKindOfClass:NSDictionary.class]) {
NSMutableDictionary *d = [NSMutableDictionary dictionary];
for (id key in obj) {
NSString *stringKey = key;
if (![key isKindOfClass:[NSString class]]) {
stringKey = [key description];
NSLog(@"property key error:%@",stringKey);
}
id v = [self convertToJson:obj[key]];
d[stringKey] = v;
}
return [NSDictionary dictionaryWithDictionary:d];
}
NSString *s = [obj description];
NSLog(@"property values error:%@", s);
return s;
}
static NSLock *objectLocker;
+(NSLock *) getObjLocker {
static dispatch_once_t onceObjLocler;
dispatch_once(&onceObjLocler, ^{
objectLocker = [[NSLock alloc] init];
});
return objectLocker;
}
static void *queueSpecKey = nil;
+(dispatch_queue_t) getSerQueue {
static dispatch_once_t once;
static dispatch_queue_t _request_queue;
dispatch_once(&once, ^{
_request_queue = dispatch_queue_create([[NSString stringWithFormat:@"com.caid.%@", self] UTF8String], NULL);
void *nonNullUnusedPointer = (__bridge void *)self;
queueSpecKey = &queueSpecKey;
dispatch_queue_set_specific(_request_queue, queueSpecKey, nonNullUnusedPointer, NULL);
});
return _request_queue;
}
static bool check_nsstring(NSString *arg) {
if (!arg || ![arg isKindOfClass:[NSString class]]
|| ![arg respondsToSelector:@selector(length)] || arg.length == 0) {
return false;
}
return true;
}
#pragma mark params
//device model
+ (NSString *) getDeviceModel {
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *machine = malloc(size);
sysctlbyname("hw.machine", machine, &size, NULL, 0);
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
return platform;
}
//device system version
+ (NSString *) getSystemVersion {
return [NSString stringWithFormat:@"%@",[UIDevice currentDevice].systemVersion];
}
//file system size
+(NSString *)getDiskSize {
int64_t space = -1;
NSError *error = nil;
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
if (!error) {
space = [[attrs objectForKey:NSFileSystemSize] longLongValue];
}
if(space < 0) {
space = -1;
}
return [NSString stringWithFormat:@"%lld",space];
}
//carrier
+(NSString* )getCarrierName {
#if TARGET_IPHONE_SIMULATOR
return @"SIMULATOR";
#else
static dispatch_queue_t _queue;
static dispatch_once_t once;
dispatch_once(&once, ^{
_queue = dispatch_queue_create([[NSString stringWithFormat:@"com.carr.%@", self] UTF8String], NULL);
});
__block NSString * carr = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(_queue, ^(){
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 12.1) {
if ([info respondsToSelector:@selector(serviceSubscriberCellularProviders)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
NSArray *carrierKeysArray = [info.serviceSubscriberCellularProviders.allKeys sortedArrayUsingSelector:@selector(compare:)];
carrier = info.serviceSubscriberCellularProviders[carrierKeysArray.firstObject];
if (!carrier.mobileNetworkCode) {
carrier = info.serviceSubscriberCellularProviders[carrierKeysArray.lastObject];
}
#pragma clang diagnostic pop
}
}
if(!carrier) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
carrier = info.subscriberCellularProvider;
#pragma clang diagnostic pop
}
if (carrier != nil) {
NSString *networkCode = [carrier mobileNetworkCode];
NSString *countryCode = [carrier mobileCountryCode];
if (countryCode && [countryCode isEqualToString:@"460"] && networkCode) {
if ([networkCode isEqualToString:@"00"] || [networkCode isEqualToString:@"02"] || [networkCode isEqualToString:@"07"] || [networkCode isEqualToString:@"08"]) {
carr= @"中国移动";
}
if ([networkCode isEqualToString:@"01"] || [networkCode isEqualToString:@"06"] || [networkCode isEqualToString:@"09"]) {
carr= @"中国联通";
}
if ([networkCode isEqualToString:@"03"] || [networkCode isEqualToString:@"05"] || [networkCode isEqualToString:@"11"]) {
carr= @"中国电信";
}
if ([networkCode isEqualToString:@"04"]) {
carr= @"中国卫通";
}
if ([networkCode isEqualToString:@"20"]) {
carr= @"中国铁通";
}
}else {
carr = [carrier.carrierName copy];
}
}
if (carr.length <= 0) {
carr = @"unknown";
}
dispatch_semaphore_signal(semaphore);
});
dispatch_time_t t = dispatch_time(DISPATCH_TIME_NOW, 0.5* NSEC_PER_SEC);
dispatch_semaphore_wait(semaphore, t);
return [carr copy];
#endif
}
//system update time
+(NSString *)getSysU {
NSString *result = nil;
NSString *information = @"L3Zhci9tb2JpbGUvTGlicmFyeS9Vc2VyQ29uZmlndXJhdGlvblByb2ZpbGVzL1B1YmxpY0luZm8vTUNNZXRhLnBsaXN0";
NSData *data=[[NSData alloc]initWithBase64EncodedString:information options:0];
NSString *dataString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:dataString error:&error];
if (fileAttributes) {
id singleAttibute = [fileAttributes objectForKey:NSFileCreationDate];
if ([singleAttibute isKindOfClass:[NSDate class]]) {
NSDate *dataDate = singleAttibute;
result = [NSString stringWithFormat:@"%f",[dataDate timeIntervalSince1970]];
}
}
return result;
}
//system boot time
+(NSString *) getSysB {
struct timeval boottime;
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
size_t size = sizeof(boottime);
time_t uptime = -1;
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0)
{
uptime = boottime.tv_sec;
}
NSString *result = [NSString stringWithFormat:@"%ld",uptime];
return result;
}
//device user name
+(NSString *) getDeviceUName {
NSString *result = [NSString stringWithFormat:@"%@",[UIDevice currentDevice].name];
return [[self class] getMD5:result ? result : @"unknown"];
}
//device RAM
+(NSString *)getRAM {
unsigned long long physicalMemory = [[NSProcessInfo processInfo] physicalMemory];
NSString *strResult = [NSString stringWithFormat:@"%llu",physicalMemory];
return [strResult copy];
}
//country coude
+(NSString *)getCountryCode {
NSLocale *currentLocale = [NSLocale currentLocale];
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
return countryCode;
}
+(NSString *)getStringTotal {
NSString *disk = [[self class] getDiskSize];
NSString *model = [[self class] getDeviceModel];
NSString *mem = [[self class] getRAM];
NSString *v = [[self class] getSystemVersion];
NSString *sysU = [[self class] getSysU];
NSString *source1 = [NSString stringWithFormat:@"%@++%@++%@++%@++%@",disk,model,mem,v,sysU];
NSLog(@"source1:%@",source1);
NSString *param1 = [[self class] getMD5:source1 ? source1 : @"unknown"];
NSLog(@"param1:%@",param1);
NSString *uName = [[self class] getDeviceUName];
NSString *carrier = [[self class] getCarrierName];
NSString *country = [[self class] getCountryCode];
NSString *sysB = [[self class] getSysB];
NSString *source2 = [NSString stringWithFormat:@"%@++%@++%@++%@",uName,carrier,country,sysB];
NSLog(@"source2:%@",source2);
NSString *param2 = [[self class] getMD5:source2 ? source2 : @"unknown"];
NSLog(@"param2:%@",param2);
NSString *result = [NSString stringWithFormat:@"%@_%@",param1,param2];
NSLog(@"result:%@",result);
return result;
}
+(NSString *)getMD5:(NSString *)source {
if (!check_nsstring(source)) {
return nil;
}
const char *cStr = [source UTF8String];
if (cStr == NULL) {
return nil;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[result appendFormat:@"%02X", digest[i]];
}
return result;
}
#pragma mark security cache
static NSString * const caid_service = @"com.data.caid.service";
static NSString * const caid_account_source = @"com.data.caid.account.source";
+(BOOL) cacheSource:(NSString *)source {
return [[self class] set:source service:caid_service acc:caid_account_source];
}
+(NSString *) cachedSource {
return [[self class] pForService:caid_service acc:caid_account_source];
}
//setPassword:forService:account:
+ (BOOL)set:(NSString *)passwordString service:(NSString *)v acc:(NSString *)t{
OSStatus status = -1001;
NSData *password = [passwordString dataUsingEncoding:NSUTF8StringEncoding];
if (password && v && t) {
[self del:v acc:t];
NSMutableDictionary *query = [self _qForService:v acc:t];
[query setObject:password forKey:(__bridge id)kSecValueData];
status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
}
return (status == errSecSuccess);
}
//deletePasswordForService:account:
+ (BOOL)del:(NSString *)s acc:(NSString *)a {
OSStatus status = -1001;
if (s && a) {
NSMutableDictionary *query = [self _qForService:s acc:a];
status = SecItemDelete((__bridge CFDictionaryRef)query);
}
return (status == errSecSuccess);
}
//passwordForService:account
+ (NSString *)pForService:(NSString *)s acc:(NSString *)a {
OSStatus status = -1001;
if (!s || !a) {
return nil;
}
CFTypeRef result = NULL;
NSMutableDictionary *query = [self _qForService:s acc:a];
[query setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
[query setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
if (status != noErr) {
return nil;
}
NSData *resultData = (__bridge_transfer NSData *)result;
return [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
}
//_queryForService:account:
+ (NSMutableDictionary *)_qForService:(NSString *)s acc:(NSString *)a {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:3];
[dic setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
if (s) {
[dic setObject:s forKey:(__bridge id)kSecAttrService];
}
if (a) {
[dic setObject:a forKey:(__bridge id)kSecAttrAccount];
}
return dic;
}
#pragma mark public interface
//获取设备实时CAID
+(NSString *) currentCAID {
NSString *totalString = [[[self class] getStringTotal] copy];
return [NSString stringWithFormat:@"%@_%@",CAID_VERSION,totalString];
}
//获取缓存在钥匙链的 CAID
+(NSString *) cachedCAID {
__block NSString *result = nil;
if (dispatch_get_specific(queueSpecKey)) {
result = [[[self class] cachedSource] copy];
}else {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async([[self class] getSerQueue], ^{
result = [[[self class] cachedSource] copy];
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, .5f* NSEC_PER_SEC));
}
return result;
}
+(void)cacheCAID:(NSString *)current {
if (!check_nsstring(current)) {
NSLog(@"function cacheCAID: param is invalid!!");
return;
}
NSString *currentCAID = [current copy];
dispatch_async([[self class] getSerQueue], ^{
[[self class] cacheSource:currentCAID];
});
}
@end