Last active
August 29, 2015 14:23
-
-
Save rosslebeau/7e2265990fb3ed997a9c to your computer and use it in GitHub Desktop.
How to make a signed request URL for Google Maps for Work API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <CommonCrypto/CommonDigest.h> | |
#import <CommonCrypto/CommonHMAC.h> | |
+ (NSString *)signedUrlFromUrl:(NSString *)fullURL { | |
fullURL = [fullURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
NSString *unmodifiedKey = [@"Your Google Maps for Work API Key" stringByReplacingOccurrencesOfString:@"-" withString:@"+"]; | |
unmodifiedKey = [unmodifiedKey stringByReplacingOccurrencesOfString:@"_" withString:@"/"]; | |
NSData *keyData = [[NSData alloc] initWithBase64EncodedData:[unmodifiedKey dataUsingEncoding:NSUTF8StringEncoding] options:0]; | |
NSData *urlData = [fullURL dataUsingEncoding:NSUTF8StringEncoding]; | |
unsigned char hmac[CC_SHA1_DIGEST_LENGTH]; | |
CCHmac(kCCHmacAlgSHA1, [keyData bytes], keyData.length, [urlData bytes], urlData.length, &hmac); | |
NSData *hashedData = [NSData dataWithBytes:hmac length:CC_SHA1_DIGEST_LENGTH]; | |
NSString *base64String = [hashedData base64EncodedStringWithOptions:0]; | |
base64String = [base64String stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; | |
NSString *modifiedBase64String = [base64String stringByReplacingOccurrencesOfString:@"+" withString:@"-"]; | |
modifiedBase64String = [modifiedBase64String stringByReplacingOccurrencesOfString:@"/" withString:@"_"]; | |
NSString *signedURL = [fullURL stringByAppendingFormat:@"&signature=%@", modifiedBase64String]; | |
return signedURL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment