介面加密處理 和 IPV6 問題

weixin_34208283發表於2017-03-23

專案中用到的 介面 加密,程式碼如下:

// get 請求 把引數都拼到URL 後面
 if ([url rangeOfString:@"?"].location != NSNotFound) {
        NSArray *array = [url componentsSeparatedByString:@"?"];
        urlStr = array[1];

    }
   NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSLog(@"%@",dateStr);
    // URL 加當前日期,和後臺約定好的 口令
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",urlStr,dateStr,@"123456"];
    NSString *signMD5 = [self md5:signStr];
// 設定請求頭
    [mgr.requestSerializer setValue:signMD5 forHTTPHeaderField:@"sign"];
    [mgr.requestSerializer setValue:dateStr forHTTPHeaderField:@"timestamp"];

// post 請求
  NSString *urlStr = @"";
    if (params) {
        NSLog(@"%@",params);
        NSArray *keys = params.allKeys;
        
        for (int i= 0; i < keys.count; i ++) {
            if (i == 0) {
                NSString *str = [NSString stringWithFormat:@"%@=%@",keys[i],params[keys[i]]];
               urlStr = [urlStr stringByAppendingString:str];
            }else{
                NSString *str = [NSString stringWithFormat:@"&%@=%@",keys[i],params[keys[i]]];
               urlStr  = [urlStr stringByAppendingString:str];
            }
        }
    }
    
    
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSLog(@"%@",dateStr);
    
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",urlStr,dateStr,@"123456"];
    NSString *signMD5 = [self md5:signStr];
    NSLog(@"%@------%@",signStr,signMD5);

// 保證好後臺生成的 MD5一致

//md5 32位 加密 (小寫)
+ (NSString *)md5:(NSString *)str {
    onst char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [[NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ] lowercaseString];
}

由於IPV6 的問題被稽核拒了,查閱資料後 在網路請求的時候 做了些處理,稽核通過了

   // ipv6
    mgr.responseSerializer.acceptableContentTypes = nil;//[NSSet setWithObject:@"text/ plain"];
    mgr.securityPolicy = [AFSecurityPolicy defaultPolicy];
    mgr.securityPolicy.allowInvalidCertificates = YES;//忽略https證書
    mgr.securityPolicy.validatesDomainName = NO;//是否驗證域名

相關文章