Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nishabe/7f2dce33772e81ff98fe77287ce7cd17 to your computer and use it in GitHub Desktop.
Save nishabe/7f2dce33772e81ff98fe77287ce7cd17 to your computer and use it in GitHub Desktop.
OBJC: HMAC creation - Pass Key and Data as String
-(NSString*)generateHMACwith :(NSString* )key :(NSString*)data{
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
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 base64EncodedStringWithOptions:0];
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment