微信功能的模块,简化一下完成了微信后台签到机制。剩下的就是网络请求了,这代码简单易懂,已经用最粗俗的码山来完成的了~~哈哈
原始签名Sign
①产生随机数
- (NSString *)acrRandow
{
NSString * randow = [[NSStringalloc]init];
int xx =0;
for (int x =0 ; x <16; x++) {
xx = arc4random() %10;
NSString * aa = [NSStringstringWithFormat:@"%d",xx];
randow = [NSStringstringWithFormat:@"%@%@",randow,aa];
}
return randow;
}
②Sign生成过程
- (NSString *)Sign
{
//对比字段参数的大小然后排序
NSArray * array = [[NSArrayalloc]initWithObjects:@"参数1",@"参数2",@"参数3",@"参数4",nil];
NSStringCompareOptions comparisonOptions =NSCaseInsensitiveSearch|NSNumericSearch|
NSWidthInsensitiveSearch|NSForcedOrderingSearch;
//根据ASCII排序
NSComparator sort = ^(NSString *obj1,NSString *obj2){
NSRange range =NSMakeRange(0,obj1.length);
return [obj1compare:obj2options:comparisonOptionsrange:range];
};
NSArray *resultArray2 = [arraysortedArrayUsingComparator:sort];
NSLog(@"字符串数组排序结果%@",resultArray2);
//原始数据
NSMutableDictionary * dic = [[NSMutableDictionaryalloc]init];
//idvf
NSString *idfv = [[[UIDevicecurrentDevice]identifierForVendor]UUIDString];
[dic setObject:[NSStringstringWithFormat:@"%@",idfv]forKey:@"DeviceId"];
//授权号
[dic setObject:@"123"forKey:@"参数1"];
//POS终端版本号
[dic setObject:@"1.0.0"forKey:@"AppVersion"];
NSString * aa = [[NSStringalloc]init];
//随机字符串
aa = [selfacrRandow];
[dic setObject:aaforKey:@"Nonce"];
//拼接串 key1=value1
NSMutableArray * arraya = [[NSMutableArrayalloc]init];
NSString * Sign = [[NSStringalloc]init];
for (int i =0; i < resultArray2.count; i++) {
NSString * Signa = [NSStringstringWithFormat:@"%@=%@",[resultArray2objectAtIndex:i],[dicobjectForKey:[resultArray2objectAtIndex:i]]];
[arraya addObject:Signa];
}
// 原始 格式 sign = key1=value1&key2=value2&....
for (int i =0; i < arraya.count; i++) {
Sign = [NSStringstringWithFormat:@"%@&%@",Sign,[arrayaobjectAtIndex:i]];
}
Sign = [Sign substringFromIndex:1];
NSLog(@"%@",Sign);
return Sign;
}
//把之前得到的Sign用 md5加密
- (NSString *)md5:(NSString *)str
{
constchar *concat_str = [strUTF8String];
unsignedchar result[CC_MD5_DIGEST_LENGTH];
CC_MD5(concat_str, (unsignedint)strlen(concat_str), result);
NSMutableString *hash = [NSMutableStringstring];
for (int i =0; i <16; i++){
[hash appendFormat:@"%02X", result[i]];
}
return [hashuppercaseString];
}
通过以上步骤就获得了一个加密之后的Sign签名