Skip to content

Instantly share code, notes, and snippets.

@eiel
Created December 3, 2013 06:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiel/7764850 to your computer and use it in GitHub Desktop.
Save eiel/7764850 to your computer and use it in GitHub Desktop.
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>
+ (NSString*)hmacsha1Hexdigest:(NSString*)data key:(NSString*)key
{
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), digest);
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment