Skip to content Skip to sidebar Skip to footer

Progressive Hmac Sha256 In Objective-c

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.key

Solution 1:

HMAC-SHA256 sample code:

+ (NSData *)hmacSha256:(NSData *)dataIn
               key:(NSData *)key
{
    NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
    CCHmac( kCCHmacAlgSHA256,
           key.bytes,
           key.length,
           dataIn.bytes,
           dataIn.length,
           macOut.mutableBytes);

    return macOut;
}

Notes:

  1. Add Security.framework to the project
  2. Common Crypto must be included: #import <CommonCrypto/CommonCrypto.h>

  3. 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];

Post a Comment for "Progressive Hmac Sha256 In Objective-c"