I need to generate a hash using HMAC SHA256. I am using the following code in JavaScript. I need an equivalent code in Objective-C.
function serialize( obj ) {
return Object.keys(obj).reduce(function(a,k){a.push(k+'='+encodeURIComponent(obj[k]));return a},[]).join('&')
}
var query = {
Action : 'MyAction',
SignatureMethod : 'HmacSHA256',
};
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, 'MYVALUE');
var queryString = ['POST', 'm.service.it', '/api/v2', serialize(sorted)].join('\n');
hmac.update(queryString);
query.Signature = CryptoJS.enc.Base64.stringify(hmac.finalize());
How implement this in Objective-C?
HMAC-SHA256 sample code:
Notes:
Security.framework
to the projectCommon Crypto must be included:
#import <CommonCrypto/CommonCrypto.h>
This is data in and out, add any conversions to desired representations before and after.
Conversions could be string to data on input and data to Base64 on output:
NSData *data = [@"string" dataUsingEncoding:NSUTF8StringEncoding];
NSString *string = [data base64EncodedStringWithOptions:0];