美文网首页iOS提升开发效率iOS杂技
iOS 开发 网络请求添加签名

iOS 开发 网络请求添加签名

作者: NewLand | 来源:发表于2016-05-25 12:16 被阅读1007次

签名算法要求:

请求参数增加、signStr,其中signTime为当前时间戳、signStr为16位随机字符串。

第一步,将所有请求参数按照参数名ASCII码从小到大排序(字典序),再使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串stringA。

第二步,在stringA最后拼接上key得到stringSignTemp字符串,并对stringSignTemp进行MD5运算,再将得到的字符串所有字符转换为大写,得到sign值signValue。

注意: 签名算法进行签名后作为发送请求时的一个参数,

比如:发送请求时参数为NSDictionary*dict = @{ @"id" :@"10",

@"page":@"1",

@"signTime":@"1464147000",

@"signStr":@"c1bhbqiolpn6xs31"};

对参数进行签名

NSString *signStr = [xx encoingWithDic:dict   Withcharacter:@"c1bhbqiolpn6xs31"];

把返回的签名作为其中一个参数

[dict setObject:signStr forKey:@"sign"]

接下来就是用这个合成的字典进行请求数据啦!

/** *  加密url

   dataDic  要进行签名的字典

   character  随机16位数 

*/

+(NSString*)encoingWithDic:(NSMutableDictionary*)dataDic Withcharacter:(NSString*)character

{   

  //当前时间戳  

NSDate *datenow = [NSDate date];   

NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]]; 

  [dataDic setObject:timeSp forKey:@"signTime"];   

//随机16位数   

//NSString *randomStr = [NSString generateFradomCharacter]; 

  [dataDic setObject:character forKey:@"signStr"];   

/*请求参数按照参数名ASCII码从小到大排序*/   

NSArray *keys = [dataDic allKeys];   

//按字母顺序排序   

NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)

{        return [obj1 compare:obj2 options:NSNumericSearch]; 

  }];   

NSString *returnStr = @"";  

//拼接字符串   

for (int i=0;i<sortedArray.count;i++){

NSString *category = sortedArray[i];

if (i==0) {

returnStr =[NSString stringWithFormat:@"%@=%@",category,dataDic[category]];}else{

returnStr = [NSString stringWithFormat:@"%@&%@=%@",returnStr,category,dataDic[category]];}

     }

/*拼接上key得到stringSignTemp*/

returnStr = [NSString stringWithFormat:@"%@&key=%@",returnStr,urlKey];

/*md5加密*/

returnStr = [self bigmd5:returnStr];

}

//md5 32位加密 (大写)

+(NSString *)bigmd5:(NSString *)str {

const char *cStr = [str UTF8String];

unsigned char result[16];

CC_MD5( cStr, strlen(cStr), result );

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]

];

}

//md5 32位 加密 (小写)

+ (NSString *)md5:(NSString *)str {

const char *cStr = [str UTF8String];

unsigned char result[32];

CC_MD5( cStr, strlen(cStr), result );

return [NSString stringWithFormat:

@"xxxxxxxxxxxxxxxx",

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],

result[16], result[17],result[18], result[19],

result[20], result[21],result[22], result[23],

result[24], result[25],result[26], result[27],

result[28], result[29],result[30], result[31]];

}

//产生16位随机数

+ (NSString *)generateFradomCharacter

{

static int kNumber = 16;

NSString *sourceStr = @"abcdefghijklmnopqrstuvwxyz0123456789";

NSMutableString *resultStr = [[NSMutableString alloc] init];

for (int i = 0; i < kNumber; i++)

{

unsigned index =  arc4random() % [sourceStr length];

NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];

[resultStr appendString:oneStr];

}

return resultStr;

}

希望对大家有用!

相关文章

网友评论

  • Jenny_e668:请问这个签名怎么用啊?
    NewLand:@Jenny_e668 在网络请求中使用,这个要和服务端约定的
    Jenny_e668:@NewLand 因为之前没用过,不知道怎么用,在什么地方用,你有写过demo或者网络请求的相关代码可以看一下吗?
    NewLand:给网络请求添加一层保障
  • 被偏爱的总有恃无恐:你好,我想问下网络请求时添加签名算法的目的是为了什么呢?在网上很少见到签名算法的资料呀
    50b8bcf22f14:防破解,防恶意请求,防爬虫
  • 901f88ca1f96:不错呢!

本文标题:iOS 开发 网络请求添加签名

本文链接:https://www.haomeiwen.com/subject/pmctdttx.html