把字典的key value 拼接成字符串加上签名加密

时间:2022-11-27 14:00:08

- (NSString *)getSianKeyWithDic:(NSDictionary *)dic

{

//按字典排序

NSArray* arr = [dic allKeys];

arr = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){

NSComparisonResult result = [obj1 compare:obj2];

return result==NSOrderedDescending;

}];

//拼接字符串

NSMutableArray * strArray =[[NSMutableArray alloc]init];

[arr enumerateObjectsUsingBlock:^(id  obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSString *appendStr = [[NSString alloc] init];

appendStr = obj;

NSString *par = dic[obj];

if (dic[obj]!=NULL) {

appendStr = [appendStr stringByAppendingString:par];

[strArray addObject:appendStr];

}

}];

NSString * str = [strArray componentsJoinedByString:@""];

NSString *sianStr = [NSString stringWithFormat:@"%@signKey",str];

NSLog(@"+++%@",sianStr);

//加密

NSString *md5Str =[self getMd5_32Bit_String:sianStr];

NSLog(@"%@",md5Str);

return md5Str;

}

//32位MD5加密方式

- (NSString *)getMd5_32Bit_String:(NSString *)srcString

{

const char *cStr = [srcString UTF8String];

unsigned char digest[CC_MD5_DIGEST_LENGTH];

NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

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

[result appendFormat:@"%02x", digest[i]];

return result;

}