Skip to content

Instantly share code, notes, and snippets.

@tobiastom
Created November 5, 2009 23:43
Show Gist options
  • Save tobiastom/227520 to your computer and use it in GitHub Desktop.
Save tobiastom/227520 to your computer and use it in GitHub Desktop.
- (NSString *)signatureBaseStringForMethod:(NSString *)aMethod
url:(NSURL *)aURL
parameters:(NSDictionary *)aParameterDict {
NSMutableDictionary *parameters = [aParameterDict mutableCopy];
if ([aURL query]) {
NSArray *parameterItems = [[aURL query] componentsSeparatedByString:@"&"];
for(NSString *parameterItem in parameterItems) {
NSMutableArray *parameterItemParts = (NSMutableArray *)[parameterItem componentsSeparatedByString:@"="];
NSString *parameter = [[parameterItemParts objectAtIndex:0] retain];
[parameterItemParts removeObjectAtIndex:0];
NSString *value = [parameterItemParts componentsJoinedByString:@"="];
[parameters setValue:value forKey:parameter]; // todo: urldecode?
}
}
NSMutableArray *normalizedParameter = [[NSMutableArray alloc] init];
NSArray *sortedKeys = [[parameters allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for(NSString *key in sortedKeys) {
[normalizedParameter addObject:[NSString stringWithFormat:@"%@=%@", key, [parameters valueForKey:key]]];
}
NSMutableString *normalizedUrl = [[NSMutableString alloc] initWithFormat:@"%@://%@", [aURL.scheme lowercaseString], [aURL.host lowercaseString]];
if (aURL.port && (([aURL.scheme isEqual:@"https"] && aURL.port != [NSNumber numberWithInt:443]) || ([aURL.scheme isEqual:@"http"] && aURL.port != [NSNumber numberWithInt:80]))) {
[normalizedUrl appendFormat:@":%d", aURL.port];
}
[normalizedUrl appendString:aURL.path];
NSString *normalizedBaseString = [NSString stringWithFormat:@"%@&%@&%@", [aURL.scheme uppercaseString], normalizedUrl, [normalizedParameter componentsJoinedByString:@"&"]];
[parameters release];
[normalizedParameter release];
[normalizedUrl release];
return normalizedBaseString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment