I’m doing the following in Java, which I would like to do the equivalent in Objective-C (minus the Base64 bit, which I already got working):
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(PRIVATE_KEY.getBytes(),"HmacSHA1");
mac.init(secret);
result = Base64.encodeToString(mac.doFinal(data), Base64.DEFAULT);
Are there any Objective-C libraries that can help me do the equivalent?
* UPDATE *
Just an update – I got the following code running, but the out comes out null:
NSData *keyData = [PRIVATE_KEY dataUsingEncoding:NSUTF8StringEncoding];
NSData *clearTextData = [data dataUsingEncoding:NSUTF8StringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CCHmacContext hmacContext;
CCHmacInit(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length);
CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
CCHmacFinal(&hmacContext, digest);
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
NSLog(@"encrypted data: %@", [NSString stringWithUTF8String:[out bytes]]);
I recently worked on another project that did HMAC-SHA1 on iPhone. Here you go!
The secret key is Base64 encoded in a NSString called secretKey
The string to sign is in the NSString called signString. If you already have NSData, just use that instead of clearTextData.
The output signature will be in base64Enc, or simply ‘out’ if you don’t want it encoded.