接口加密处理 和 IPV6 问题

项目中用到的 接口 加密,代码如下:

// 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;//是否验证域名

你可能感兴趣的:(接口加密处理 和 IPV6 问题)