Skip to content

Instantly share code, notes, and snippets.

@yangboz
Created March 23, 2013 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangboz/77fffef6b6d4fe16b144 to your computer and use it in GitHub Desktop.
Save yangboz/77fffef6b6d4fe16b144 to your computer and use it in GitHub Desktop.
Hash-based message authentication code Ref:http://en.wikipedia.org/wiki/HMAC
- (NSString*) HMACWithSecret:(NSString*)secret andData:(NSString *)data
{
CCHmacContext ctx;
const char *key = [secret UTF8String];
const char *str = [data UTF8String];
unsigned char mac[CC_SHA256_DIGEST_LENGTH];
char hexmac[CC_SHA256_DIGEST_LENGTH];
char *p;
CCHmacInit( &ctx, kCCHmacAlgSHA256, key, strlen( key ));
CCHmacUpdate( &ctx, str, strlen(str) );
CCHmacFinal( &ctx, mac );
p = hexmac;
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++ ) {
snprintf( p, 3, "%02x", mac[ i ] );
p += 2;
}
return [NSString stringWithUTF8String:hexmac];
}
@yangboz
Copy link
Author

yangboz commented Nov 26, 2013

2013-11-26 17:16:07.704 ChatBotsMessager[547:60b] API request response str:{"success":0,"errorType":"Missing parameter","errorMessage":"The hash parameter must be passed in the url."}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment