Following is the code:
- (IBAction)postBtnTapped:(id)sender {
NSString *accessKey = @"SERVER_ACCESS_KEY";
NSString *secretKey=@"SERVER_SECRET_KEY";
UIImage *img =[[UIImage alloc] initWithContentsOfFile:@"stones.png"];
NSData *data = UIImagePNGRepresentation(img);
NSString *imageDataString = [data base64Encoding]; //// I have used the Category to find out the base64encoded String
NSString *imgWidth = [[NSNumber numberWithFloat:img.size.width] stringValue];
NSDictionary * postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"ASW_Image",@"name", img.size.width,@"width" , imageDataString,@"image", nil];
NSError * error = nil;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONReadingMutableContainers error:&error];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://vws.vuforia.com/targets"]];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:jsonData];
NSDate *currDate = [NSDate date];
NSString *dateStr = [self getUTCFormateDate:currDate];
NSLog(@"DateFormat: %@",dateStr);
[urlRequest setValue: dateStr forHTTPHeaderField:@"Date"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *contentmd5 =@"I am having trouble to find this String in Objective-C"
NSString *dataValue =[NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@",urlRequest.HTTPMethod,contentmd5,@"application/json",dateStr,urlRequest.URL.path];
NSString *hmacStr = [self HMAC:secretKey :dataValue];
NSString *authStr = [NSString stringWithFormat:@"VWS %@:%@",accessKey,hmacStr];
[urlRequest setValue:authStr forHTTPHeaderField:@"Authorization"];
NSURLConnection * myConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
}
-(NSString*)HMAC:(NSString*)key:(NSString*)data{
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSString *hash = [HMAC base64Encoding]; //// I have used the Category to find out the base64encoded String
return hash;
}
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
return dateString;
}
Please give some time to see the code.Am I doing wrong anything? Also I want to ask what will be the input to find out the contentMD5 string?
DId you guys ever find a solution here?
I've got similar code to above, however I am trying with a GET approach, and therefore remove an chance it is the objective-c MD5 algorithm. As such, I believe that this must be due to the HMAC routine?
If there is anyone on this forum, that knows enough objective-c to validate the signing approach here, that would be of huge assistance. My code is as follows: