NSString * timeC = [NSString stringWithFormat:@"%ld",time(NULL)];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"4886659db1dfc63a05583503aff1ce9454d01bfc",@"key",timeC,@"time",@"ios",@"facility",nil];
NSArray *allKeyArray = [dict allKeys];
NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return NSOrderedDescending;
}];
//通过排列的key值获取value
NSMutableArray *valueArray = [NSMutableArray array];
for (NSString *sortsing in afterSortKeyArray) {
NSString *valueString = [dict objectForKey:sortsing];
[valueArray addObject:valueString];
}
NSMutableArray *signArray = [NSMutableArray array];
for (int i = 0 ; i < afterSortKeyArray.count; i++) {
NSString * keyValue = [NSString stringWithFormat:@"%@=%@",afterSortKeyArray[i],valueArray[i]];
[signArray addObject:keyValue];
}
NSString *str = @"";
for (NSString *temp in signArray) {
str = [str stringByAppendingString:[NSString stringWithFormat:@"%@&",temp]];
}
NSString *shalStr = [str substringToIndex:str.length - 1];
NSLog(@"shlstr = %@",shalStr);
// shalStr = [shalStr sha1String];
shalStr = [self getSha1String:shalStr];
NSLog(@"sign = %@", shalStr);
//sha1加密
//sha1加密方式
+ (NSString *)getSha1String:(NSString *)srcString{
const char *cstr = [srcString cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:srcString.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString* result = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++) {
[result appendFormat:@"%02x", digest[i]];
}
return result;
}
网友评论