RQHTTPService+RQGetPlaceList.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // RQHTTPService+RQGetPlaceList.m
  3. // JiaPei
  4. //
  5. // Created by 张嵘 on 2023/4/14.
  6. // 查询考场信息列表
  7. #import "RQHTTPService+RQGetPlaceList.h"
  8. #include <CommonCrypto/CommonCrypto.h>
  9. @implementation RQPlaceListModel
  10. - (void)checkWithComplete:(VoidBlock_Bool)complete {
  11. __block BOOL isVerify;
  12. //rsa 公钥
  13. NSString *publicKey = [NSString stringWithFormat:@"%@%@%@",@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCzR04LMmDlwyVRfK+Kgsm/vFMOFyqvGk4QIVC",@"Gj2CKKfhycRExm15N3tKOOouIjl/EU79It45oS+ltJkscvYrJn1zz+1bSoBLelZzJrn7K0NpHF8k", @"bCprsFHd60x+YJ4l8C3SDxfFebo4kqveJzx6JNZKwzFm7LPEO5XB9pEBRwIDAQAB"];
  14. // 构造被签名串
  15. NSMutableString *mutableVerifyString = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%@",self.userId]];
  16. if (RQStringIsNotEmpty(self.expirationTime)) {
  17. [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:self.expirationTime formatter:@"yyyy-MM-dd HH:mm:ss"]];
  18. }
  19. [mutableVerifyString appendFormat:@"%ld",self.isVip];
  20. //公钥验签
  21. isVerify = [self verify:mutableVerifyString.copy signature:RQStringIsNotEmpty(self.sign)? self.sign : @"" withPublivKey:publicKey];
  22. NSLog(@"验签通过了吧------%@", isVerify ? @"YES":@"NO");
  23. BOOL isVip = self.isVip == 1;
  24. self.isCheckVip = isVip;
  25. if (complete) {
  26. complete(isVerify? isVip : NO);
  27. }
  28. }
  29. #pragma mark - PrivateMethods
  30. // verify Signature
  31. - (BOOL)verify:(NSString *)content signature:(NSString *)signature withPublivKey:(NSString *)publicKey {
  32. SecKeyRef publicKeyRef = [self addPublicKey:publicKey];
  33. if (!publicKeyRef) { NSLog(@"添加公钥失败"); return NO; }
  34. NSData *originData = [self sha256:content];
  35. NSData *signatureData = [[NSData alloc] initWithBase64EncodedString:signature options:NSDataBase64DecodingIgnoreUnknownCharacters];
  36. if (!originData || !signatureData) { return NO; }
  37. OSStatus status = SecKeyRawVerify(publicKeyRef, kSecPaddingPKCS1SHA256, [originData bytes], originData.length, [signatureData bytes], signatureData.length);
  38. if (status ==noErr) { return YES; }
  39. else{ NSLog(@"验签失败:%d",status); return NO; }
  40. }
  41. // digest message with sha1
  42. - (NSData *)sha256:(NSString *)str {
  43. const void *data = [str cStringUsingEncoding:NSUTF8StringEncoding];
  44. CC_LONG len = (CC_LONG)strlen(data);
  45. uint8_t * md = malloc( CC_SHA256_DIGEST_LENGTH * sizeof(uint8_t) );;
  46. CC_SHA256(data, len, md);
  47. return [NSData dataWithBytes:md length:CC_SHA256_DIGEST_LENGTH];
  48. }
  49. - (SecKeyRef)addPublicKey:(NSString *)pubKey {
  50. NSData *data = [[NSData alloc] initWithBase64EncodedString:pubKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
  51. //a tag to read/write keychain storage
  52. NSString *tag = @"RSA_PUBLIC_KEY";
  53. NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
  54. // Delete any old lingering key with the same tag
  55. NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
  56. [publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
  57. [publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
  58. [publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
  59. SecItemDelete((__bridge CFDictionaryRef)publicKey);
  60. // Add persistent version of the key to system keychain
  61. [publicKey setObject:data forKey:(__bridge id)kSecValueData];
  62. [publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)kSecAttrKeyClass];
  63. [publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnPersistentRef];
  64. CFTypeRef persistKey = nil;
  65. OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
  66. if (persistKey != nil){
  67. CFRelease(persistKey);
  68. }
  69. if ((status != noErr) && (status != errSecDuplicateItem)) { return nil; }
  70. [publicKey removeObjectForKey:(__bridge id)kSecValueData];
  71. [publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
  72. [publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
  73. [publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
  74. // Now fetch the SecKeyRef version of the key
  75. SecKeyRef keyRef = nil;
  76. status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef);
  77. if(status != noErr){
  78. return nil;
  79. }
  80. return keyRef;
  81. }
  82. @end
  83. @implementation RQHTTPService (RQGetPlaceList)
  84. /**
  85. 查询考场信息列表
  86. @param pageNum 当前页码
  87. @param pageSize 每页数据量
  88. @param locationModel 定位信息
  89. @param placeName 搜索关键字
  90. @return Returns a signal which will send complete, or error.
  91. */
  92. - (RACSignal *)getPlaceListWithPageNum:(NSInteger)pageNum pageSize:(NSInteger)pageSize placeName:(NSString *)placeName {
  93. /// 1. 配置参数
  94. RQKeyedSubscript *subscript = [RQKeyedSubscript subscript];
  95. if (pageNum != 0 && pageSize != 0) {
  96. /// 当前页码
  97. subscript[@"pageNum"] = @(pageNum);
  98. /// 每页数据量
  99. subscript[@"pageSize"] = @(pageSize);
  100. }
  101. if (RQStringIsNotEmpty(placeName)) {
  102. /// 考场名称
  103. subscript[@"name"] = placeName;
  104. }
  105. /// 城市ID
  106. subscript[@"cityId"] = RQ_USER_MANAGER.currentUser.city;
  107. /// 用户id
  108. subscript[@"userId"] = RQ_USER_MANAGER.currentUser._id;
  109. /// 2. 配置参数模型
  110. RQURLParameters *paramters = [RQURLParameters urlParametersWithMethod:RQ_HTTTP_METHOD_GET path: RQ_GET_PlaceList parameters:subscript.dictionary];
  111. /// 3.发起请求
  112. return [[[RQHTTPRequest requestWithParameters:paramters]
  113. enqueueResultClass:[RQPlaceListModel class]]
  114. rq_parsedResults];
  115. }
  116. @end