SecurityLib.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // SecurityLib.m
  3. // Timi
  4. //
  5. // Created by gy on 2023/6/18.
  6. //
  7. #import "SecurityLib.h"
  8. #import "sys/utsname.h"
  9. #import "MIUAES.h"
  10. #import <CommonCrypto/CommonDigest.h>
  11. @interface SecurityLib () {
  12. }
  13. /*
  14. {"pm":"x86_64"
  15. ,"st":3,
  16. "a":11,
  17. "pn":"com.timichat.app",
  18. "c":2,
  19. "lat":"",
  20. "s":"2A06655142DEAC08B4D3B29FB9C3D053A2C4184E5DF04478C9729C0455BBB035",
  21. "sv":"16.1",
  22. "t":"",
  23. "di":"8d8359b38e4cc368e0417058b54fc179b20358bc",
  24. "lng":"",
  25. "ts":1687099133000,
  26. "ci":201,
  27. "av":"2.5.0"}
  28. */
  29. /// 1、客户端把以下这些属性,通过加密,生成pkg(headerpackage)。
  30. /// 2、客户端通过http请求设置headerpkg,服务端接收到请求拿到headerpkg,进行解密得到数据如版本号和验证token内容等
  31. /**
  32. * 应用类型
  33. */
  34. @property (nonatomic, assign) int TAppType;
  35. /**
  36. * 访问令牌
  37. */
  38. @property (nonatomic, strong) NSString *tToken;
  39. /**
  40. * 经度
  41. */
  42. @property (nonatomic, strong) NSString *tLongitude;
  43. /**
  44. * 纬度
  45. */
  46. @property (nonatomic, strong) NSString *tLatitude;
  47. /**
  48. * 软件版本号
  49. */
  50. @property (nonatomic, strong) NSString *tAppVersion;
  51. /**
  52. * 渠道号
  53. */
  54. @property (nonatomic, strong) NSNumber *tChannelId;
  55. //-----
  56. /**
  57. * 客户端类型
  58. */
  59. @property (nonatomic, assign) int tClientType;
  60. /**
  61. * 包名
  62. */
  63. @property (nonatomic, strong) NSString *tPackageName;
  64. /**
  65. * 手机型号
  66. */
  67. @property (nonatomic, strong) NSString *tPhoneModel;
  68. /**
  69. * 系统版本号
  70. */
  71. @property (nonatomic, strong) NSString *tSystemVersion;
  72. /**
  73. * 设备号
  74. */
  75. @property (nonatomic, strong) NSString *tDeviceId;
  76. /**
  77. * 时间戳
  78. */
  79. @property (nonatomic, strong) NSString *tTimestamp;
  80. /**
  81. * 签名版本
  82. */
  83. @property (nonatomic, assign) int tSignType;
  84. /**
  85. * 签名
  86. */
  87. @property (nonatomic, strong) NSString *tSign;
  88. @property (nonatomic, strong) NSString *AES_KEY;
  89. @end
  90. @implementation SecurityLib
  91. +(id)sharedInstance{
  92. static SecurityLib *instance;
  93. static dispatch_once_t once;
  94. dispatch_once(&once, ^{
  95. instance = [[SecurityLib alloc] init];
  96. });
  97. return instance;
  98. }
  99. - (id)init {
  100. self = [super init];
  101. if(self) {
  102. [self setup];
  103. }
  104. return self;
  105. }
  106. - (void)setup{
  107. self.tClientType = 2;
  108. self.tPackageName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
  109. struct utsname systemInfo;
  110. uname(&systemInfo);
  111. // 获取设备标识Identifier
  112. self.tPhoneModel= [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
  113. self.tSystemVersion = [[UIDevice currentDevice] systemVersion];;
  114. self.tDeviceId = @"123";
  115. NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
  116. NSInteger time = interval;
  117. self.tTimestamp = [NSString stringWithFormat:@"%zd",time];
  118. self.tSignType = 3;
  119. //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
  120. //self.tSign = [NSString stringWithFormat:@"a=%d&av="];
  121. self.AES_KEY = @"*TiMi#Chat@2020*";
  122. }
  123. -(void)setAppType:(int) appType{
  124. self.TAppType = appType;
  125. }
  126. -(void)setToken:(NSString*) token{
  127. self.tToken = token;
  128. }
  129. -(void)setLongitude:(NSString*) longitude{
  130. self.tLongitude = longitude;
  131. }
  132. -(void)setLatitude:(NSString*) latitude{
  133. self.tLatitude = latitude;
  134. }
  135. -(void)setLongitude:(NSString *)longitude andLatitude:(NSString *)latitude{
  136. self.tLongitude = longitude;
  137. self.tLatitude = latitude;
  138. }
  139. -(void)setAppVersion:(NSString*) appVersion{
  140. self.tAppVersion = appVersion;
  141. }
  142. -(void)setChannelId:(NSNumber*) channelId{
  143. self.tChannelId = channelId;
  144. }
  145. //----
  146. -(NSString*)getDeviceId{
  147. NSString *deviceId = @"123456";
  148. return deviceId;
  149. }
  150. -(BOOL)isFirstInstall{
  151. BOOL isFirstInstall = YES;
  152. return isFirstInstall;
  153. }
  154. //{"pm":"x86_64"
  155. //,"st":3,
  156. // "a":11,
  157. // "pn":"com.timichat.app",
  158. // "c":2,
  159. // "lat":"",
  160. // "s":"2A06655142DEAC08B4D3B29FB9C3D053A2C4184E5DF04478C9729C0455BBB035",
  161. // "sv":"16.1",
  162. // "t":"",
  163. // "di":"8d8359b38e4cc368e0417058b54fc179b20358bc",
  164. // "lng":"",
  165. // "ts":1687099133000,
  166. // "ci":201,
  167. // "av":"2.5.0"}
  168. -(NSString*)getHeaderPackage{
  169. NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] * 1000;
  170. NSInteger time = interval;
  171. self.tTimestamp = [NSString stringWithFormat:@"%zd",time];
  172. self.tPhoneModel = self.tPhoneModel?self.tPhoneModel:@"";
  173. self.tPackageName = self.tPackageName?self.tPackageName:@"";
  174. self.tLatitude = self.tLatitude?self.tLatitude:@"";
  175. self.tSign = self.tSign?self.tSign:@"";
  176. self.tSystemVersion = self.tSystemVersion?self.tSystemVersion:@"";
  177. self.tToken = self.tToken?self.tToken:@"";
  178. self.tDeviceId = self.tDeviceId?self.tDeviceId:@"";
  179. self.tLongitude = self.tLongitude?self.tLongitude:@"";
  180. self.tTimestamp = self.tTimestamp?self.tTimestamp:@"";
  181. self.tAppVersion = self.tAppVersion?self.tAppVersion:@"";
  182. //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
  183. //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
  184. //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
  185. //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
  186. NSString *str = @"";
  187. // sbToSign.append("a=" + (appType == null ? "" : appType.getValue()));
  188. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"a=%d",self.TAppType]];
  189. // sbToSign.append("&av=" + Optional.ofNullable(appVersion).orElse(""));
  190. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&av=%@",self.tAppVersion]];
  191. // sbToSign.append("&c=" + (clientType == null ? "" : clientType.getValue()));
  192. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&c=%d",self.tClientType]];
  193. // sbToSign.append("&ci=" + (channelId == null ? "" : channelId));
  194. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&ci=%d",[self.tChannelId intValue]]];
  195. // sbToSign.append("&di=" + Optional.ofNullable(deviceId).orElse(""));
  196. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&di=%@",self.tDeviceId]];
  197. // sbToSign.append("&lat=" + Optional.ofNullable(latitude).orElse(""));
  198. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&lat=%@",self.tLatitude]];
  199. // sbToSign.append("&lng=" + Optional.ofNullable(longitude).orElse(""));
  200. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&lng=%@",self.tLongitude]];
  201. // sbToSign.append("&pm=" + Optional.ofNullable(phoneModel).orElse(""));
  202. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&pm=%@",self.tPhoneModel]];
  203. // sbToSign.append("&pn=" + Optional.ofNullable(packageName).orElse(""));
  204. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&pn=%@",self.tPackageName]];
  205. // sbToSign.append("&st=" + (signType == null ? "" : signType));
  206. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&st=%d",self.tSignType]];
  207. // sbToSign.append("&sv=" + Optional.ofNullable(systemVersion).orElse(""));
  208. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&sv=%@",self.tSystemVersion]];
  209. // sbToSign.append("&t=" + Optional.ofNullable(token).orElse(""));
  210. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&t=%@",self.tToken]];
  211. // sbToSign.append("&ts=" + (timestamp == null ? "" : timestamp));
  212. str = [str stringByAppendingFormat:@"%@", [NSString stringWithFormat:@"&ts=%@%@.iOS.security",self.tTimestamp,self.tPackageName]];
  213. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  214. // self.tSign = [self sha256HashFor:str];
  215. // });
  216. // dispatch_async(dispatch_get_main_queue(), ^{
  217. self.tSign = [self sha256HashFor:str];
  218. // });
  219. NSDictionary *dict = @{@"pm":self.tPhoneModel,
  220. @"st":[NSNumber numberWithInt:self.tSignType],
  221. @"a":[NSNumber numberWithInt:self.TAppType],
  222. @"pn":self.tPackageName,
  223. @"c":[NSNumber numberWithInt:self.tClientType],
  224. @"lat":self.tLatitude,
  225. @"s":self.tSign,
  226. @"sv":self.tSystemVersion,
  227. @"t":self.tToken,
  228. @"di":self.tDeviceId,
  229. @"lng":self.tLongitude,
  230. @"ts":self.tTimestamp,
  231. @"ci":self.tChannelId,
  232. @"av":self.tAppVersion};
  233. NSString *headerPackage = [self dictionaryToJson:dict];
  234. NSString *enStr = [MIUAES MIUAESEncrypt:headerPackage mode:kCCModeECB key:self.AES_KEY keySize:MIUKeySizeAES128 iv:headerPackage padding:MIUCryptorPKCS7Padding];
  235. return enStr;
  236. }
  237. - (NSString*)dictionaryToJson:(NSDictionary *)dic{
  238. NSError *parseError = nil;
  239. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
  240. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  241. }
  242. //SHA256加密
  243. - (NSString*)sha256HashFor:(NSString*)input{
  244. const char* str = [input UTF8String];
  245. unsigned char result[CC_SHA256_DIGEST_LENGTH];
  246. CC_SHA256(str, (CC_LONG)strlen(str), result);
  247. NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
  248. for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
  249. {
  250. [ret appendFormat:@"%02x",result[i]];
  251. }
  252. ret = (NSMutableString *)[ret lowercaseString];
  253. return ret;
  254. }
  255. @end