123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- //
- // SecurityLib.m
- // Timi
- //
- // Created by gy on 2023/6/18.
- //
- #import "SecurityLib.h"
- #import "sys/utsname.h"
- #import "MIUAES.h"
- #import <CommonCrypto/CommonDigest.h>
- @interface SecurityLib () {
-
- }
- /*
- {"pm":"x86_64"
- ,"st":3,
- "a":11,
- "pn":"com.timichat.app",
- "c":2,
- "lat":"",
- "s":"2A06655142DEAC08B4D3B29FB9C3D053A2C4184E5DF04478C9729C0455BBB035",
- "sv":"16.1",
- "t":"",
- "di":"8d8359b38e4cc368e0417058b54fc179b20358bc",
- "lng":"",
- "ts":1687099133000,
- "ci":201,
- "av":"2.5.0"}
- */
- /// 1、客户端把以下这些属性,通过加密,生成pkg(headerpackage)。
- /// 2、客户端通过http请求设置headerpkg,服务端接收到请求拿到headerpkg,进行解密得到数据如版本号和验证token内容等
- /**
- * 应用类型
- */
- @property (nonatomic, assign) int TAppType;
- /**
- * 访问令牌
- */
- @property (nonatomic, strong) NSString *tToken;
- /**
- * 经度
- */
- @property (nonatomic, strong) NSString *tLongitude;
- /**
- * 纬度
- */
- @property (nonatomic, strong) NSString *tLatitude;
- /**
- * 软件版本号
- */
- @property (nonatomic, strong) NSString *tAppVersion;
- /**
- * 渠道号
- */
- @property (nonatomic, strong) NSNumber *tChannelId;
- //-----
- /**
- * 客户端类型
- */
- @property (nonatomic, assign) int tClientType;
- /**
- * 包名
- */
- @property (nonatomic, strong) NSString *tPackageName;
- /**
- * 手机型号
- */
- @property (nonatomic, strong) NSString *tPhoneModel;
- /**
- * 系统版本号
- */
- @property (nonatomic, strong) NSString *tSystemVersion;
- /**
- * 设备号
- */
- @property (nonatomic, strong) NSString *tDeviceId;
- /**
- * 时间戳
- */
- @property (nonatomic, strong) NSString *tTimestamp;
- /**
- * 签名版本
- */
- @property (nonatomic, assign) int tSignType;
- /**
- * 签名
- */
- @property (nonatomic, strong) NSString *tSign;
- @property (nonatomic, strong) NSString *AES_KEY;
- @end
- @implementation SecurityLib
- +(id)sharedInstance{
- static SecurityLib *instance;
- static dispatch_once_t once;
- dispatch_once(&once, ^{
- instance = [[SecurityLib alloc] init];
- });
- return instance;
- }
- - (id)init {
- self = [super init];
- if(self) {
- [self setup];
- }
- return self;
- }
- - (void)setup{
- self.tClientType = 2;
- self.tPackageName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
-
- struct utsname systemInfo;
- uname(&systemInfo);
- // 获取设备标识Identifier
- self.tPhoneModel= [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
-
- self.tSystemVersion = [[UIDevice currentDevice] systemVersion];;
- self.tDeviceId = @"123";
-
- NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
- NSInteger time = interval;
- self.tTimestamp = [NSString stringWithFormat:@"%zd",time];
-
- self.tSignType = 3;
- //a=11&av=2.5.0&c=2&ci=2&di=123&lat=&lng=&pm=x86_64&pn=com.timichat.app&st=3&sv=16.1&t=&ts=1687453091143com.timichat.app.iOS.security
- //self.tSign = [NSString stringWithFormat:@"a=%d&av="];
-
- self.AES_KEY = @"*TiMi#Chat@2020*";
- }
- -(void)setAppType:(int) appType{
- self.TAppType = appType;
- }
- -(void)setToken:(NSString*) token{
- self.tToken = token;
- }
- -(void)setLongitude:(NSString*) longitude{
- self.tLongitude = longitude;
- }
- -(void)setLatitude:(NSString*) latitude{
- self.tLatitude = latitude;
- }
- -(void)setLongitude:(NSString *)longitude andLatitude:(NSString *)latitude{
- self.tLongitude = longitude;
- self.tLatitude = latitude;
- }
- -(void)setAppVersion:(NSString*) appVersion{
- self.tAppVersion = appVersion;
- }
- -(void)setChannelId:(NSNumber*) channelId{
- self.tChannelId = channelId;
- }
- //----
- -(NSString*)getDeviceId{
- NSString *deviceId = @"123456";
- return deviceId;
- }
- -(BOOL)isFirstInstall{
- BOOL isFirstInstall = YES;
- return isFirstInstall;
- }
- //{"pm":"x86_64"
- //,"st":3,
- // "a":11,
- // "pn":"com.timichat.app",
- // "c":2,
- // "lat":"",
- // "s":"2A06655142DEAC08B4D3B29FB9C3D053A2C4184E5DF04478C9729C0455BBB035",
- // "sv":"16.1",
- // "t":"",
- // "di":"8d8359b38e4cc368e0417058b54fc179b20358bc",
- // "lng":"",
- // "ts":1687099133000,
- // "ci":201,
- // "av":"2.5.0"}
- -(NSString*)getHeaderPackage{
- NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
- NSInteger time = interval;
- self.tTimestamp = [NSString stringWithFormat:@"%zd",time];
-
- self.tPhoneModel = self.tPhoneModel?self.tPhoneModel:@"";
- self.tPackageName = self.tPackageName?self.tPackageName:@"";
- self.tLatitude = self.tLatitude?self.tLatitude:@"";
- self.tSign = self.tSign?self.tSign:@"";
- self.tSystemVersion = self.tSystemVersion?self.tSystemVersion:@"";
- self.tToken = self.tToken?self.tToken:@"";
- self.tDeviceId = self.tDeviceId?self.tDeviceId:@"";
- self.tLongitude = self.tLongitude?self.tLongitude:@"";
- self.tTimestamp = self.tTimestamp?self.tTimestamp:@"";
- self.tAppVersion = self.tAppVersion?self.tAppVersion:@"";
- //a=11&av=2.5.0&c=2&ci=201&di=123&lat=&lng=&pm=x86_64&pn=com.timichat.app&st=3&sv=16.1&t=&ts=1687453091143com.timichat.app.iOS.security
- //a=11&av=2.5.0&c=2&ci=201&di=123&lat=&lng=&pm=x86_64&pn=com.timichat.app&st=3&sv=16.1&t=&ts=1687457876734com.timichat.app.iOS.security
- //a=11&av=2.5.0&c=2&ci=201&di=123&lat=&lng=&pm=x86_64&pn=com.timichat.app&st=3&sv=16.1&t=&ts=1687457876734com.timichat.app.iOS.security
- //a=11&av=2.5.0&c=2&ci=201&di=123&lat=&lng=&pm=x86_64&pn=com.timichat.app&st=3&sv=16.1&t=&ts=1687458122033com.timichat.app.iOS.security
- NSString *str = @"";
- // sbToSign.append("a=" + (appType == null ? "" : appType.getValue()));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"a=%d",self.TAppType]];
- // sbToSign.append("&av=" + Optional.ofNullable(appVersion).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&av=%@",self.tAppVersion]];
- // sbToSign.append("&c=" + (clientType == null ? "" : clientType.getValue()));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&c=%d",self.tClientType]];
- // sbToSign.append("&ci=" + (channelId == null ? "" : channelId));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&ci=%d",[self.tChannelId intValue]]];
- // sbToSign.append("&di=" + Optional.ofNullable(deviceId).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&di=%@",self.tDeviceId]];
- // sbToSign.append("&lat=" + Optional.ofNullable(latitude).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&lat=%@",self.tLatitude]];
- // sbToSign.append("&lng=" + Optional.ofNullable(longitude).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&lng=%@",self.tLongitude]];
- // sbToSign.append("&pm=" + Optional.ofNullable(phoneModel).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&pm=%@",self.tPhoneModel]];
- // sbToSign.append("&pn=" + Optional.ofNullable(packageName).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&pn=%@",self.tPackageName]];
- // sbToSign.append("&st=" + (signType == null ? "" : signType));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&st=%d",self.tSignType]];
- // sbToSign.append("&sv=" + Optional.ofNullable(systemVersion).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&sv=%@",self.tSystemVersion]];
- // sbToSign.append("&t=" + Optional.ofNullable(token).orElse(""));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&t=%@",self.tToken]];
- // sbToSign.append("&ts=" + (timestamp == null ? "" : timestamp));
- str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&ts=%@%@.iOS.security",self.tTimestamp,self.tPackageName]];
-
- // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- // self.tSign = [self sha256HashFor:str];
- // });
- // dispatch_async(dispatch_get_main_queue(), ^{
- self.tSign = [self sha256HashFor:str];
- // });
-
- NSDictionary *dict = @{@"pm":self.tPhoneModel,
- @"st":[NSNumber numberWithInt:self.tSignType],
- @"a":[NSNumber numberWithInt:self.TAppType],
- @"pn":self.tPackageName,
- @"c":[NSNumber numberWithInt:self.tClientType],
- @"lat":self.tLatitude,
- @"s":self.tSign,
- @"sv":self.tSystemVersion,
- @"t":self.tToken,
- @"di":self.tDeviceId,
- @"lng":self.tLongitude,
- @"ts":self.tTimestamp,
- @"ci":self.tChannelId,
- @"av":self.tAppVersion};
-
- NSString *headerPackage = [self dictionaryToJson:dict];
-
- NSString *enStr = [MIUAES MIUAESEncrypt:headerPackage mode:kCCModeECB key:self.AES_KEY keySize:MIUKeySizeAES128 iv:headerPackage padding:MIUCryptorPKCS7Padding];
-
- return enStr;
- }
- - (NSString*)dictionaryToJson:(NSDictionary *)dic{
- NSError *parseError = nil;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
- return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- }
- //SHA256加密
- - (NSString*)sha256HashFor:(NSString*)input{
- const char* str = [input UTF8String];
- unsigned char result[CC_SHA256_DIGEST_LENGTH];
- CC_SHA256(str, (CC_LONG)strlen(str), result);
-
- NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
- for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
- {
- [ret appendFormat:@"%02x",result[i]];
- }
- ret = (NSMutableString *)[ret lowercaseString];
- return ret;
- }
- @end
|