2

So finally figured out how to do an hmac sha 256 hashing. I will be using this for a wcf service api i made. My problem is that the NSData output that my method is sending out have spaces.

eg. This is how it looks like what my API sends out

"2efb00aba01a3f5b674fba3063b43fee7a9356947118......"

And this is how my iphone app shows it

<2efb00ab a01a3f5b 674fba30.....>

This is how my code in objective c looks like:

NSData *hmacSHA256(NSString *key, NSString *data)
{
    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
}

This came from this answer:

https://stackoverflow.com/a/8459123/639713

Anyway, my issue is, how do I deal with this. How do I convert the NSdata output to string? And if does get converted to string I'm guessing the output will be different from what the WCF Service API sends out. Do I change how the API processes it's hmacsha256 output?

Thanks!

Community
  • 1
  • 1
gdubs
  • 2,724
  • 9
  • 55
  • 102
  • possible duplicate of [Best way to serialize a NSData into an hexadeximal string](http://stackoverflow.com/questions/1305225/best-way-to-serialize-a-nsdata-into-an-hexadeximal-string) – googletorp Jan 26 '13 at 13:53

2 Answers2

6

You could modify your method slightly so that instead of creating an NSData containing the digest bytes, you could create a string formatting the bytes as hexadecimal.

NSString *hmacSHA256(NSString *key, NSString *data)
{
    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    NSMutableString *result = [NSMutableString string];
    for (int i = 0; i < sizeof cHMAC, i++)
    {
        [result appendFormat:@"%02hhx", cHMAC[i]];
    }

    return result;
}
dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • 1
    this is exactly it! I just saw my wcf service do the same thing like so >> for (int i = 0; i < buff.Length; i++){ sbinary += buff[i].ToString("X2"); // hex format }. I actually just followed this tutorial as well. Do you mind helpin me out understand why it needs to have to use that format? – gdubs Jan 26 '13 at 04:10
  • Thanks a lot! Just came across this same problem, and this fixed it perfectly! – julianwyz Oct 08 '14 at 16:24
1

<2efb00ab a01a3f5b 674fba30.....> looks like the result of calling -[NSData description], like NSLog would do for any %@ format strings. The NSData itself represents a sequence of bytes. The output you're after appears to be the byte sequence as a hexidecimal string. See Best way to serialize an NSData into a hexadeximal string for how to serialize the NSData to that format.

Community
  • 1
  • 1
bdash
  • 18,110
  • 1
  • 59
  • 91