I\'ve been trying to generate HMAC SHA256 for Facebook signed request on iOS for weeks now. I am desperate need of help.
Facebook signed requests have two parts which a
The problem was with the Base64 encoder. It needs to be encoded as Base64Url see: http://en.wikipedia.org/wiki/Base64#URL_applications
Here's the modifed base64EncodedString category method:
//NSData+Base64.h
- (NSString *)base64EncodedString
{
size_t outputLength;
char *outputBuffer = NewBase64Encode([self bytes], [self length], true, &outputLength);
NSString *result = [[[NSString alloc] initWithBytes:outputBuffer length:outputLength encoding:NSASCIIStringEncoding] autorelease];
free(outputBuffer);
NSString *b64PayloadClean = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@""];
//do URL encoding by replacing "+" and "/" to "-" and "_" respectively
b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"=" withString:@""];
b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
b64PayloadClean = [b64PayloadClean stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
return b64PayloadClean;
}