Printing NSString from NSData and comparing HMAC hashes

I have an NSData that I used using the HMAC algorithm. I wanted to print the given line and see how it looks, but all the time I get (zero) printed on the screen. I tried 2 methods, but they did not work. Can anyone suggest how to print a hashed data string? I would like to compare a hash created this way with a hash created on a JAVA server. Now JAVA returns a string object. How will it compare with NSData created by objective C.

NSData *hmac = [aData HMACWithAlgorithm:kCCHmacAlgSHA1];
NSString *hmacStr = [NSString stringWithUTF8String:[hmac bytes]];
NSString *hmacStr1 = [[NSString alloc] initWithData:hmac
                                                encoding:NSUTF8StringEncoding];
NSLog(@"Hashed Data=%@ data2=%@",hmacStr,hmacStr1);
+3
source share
2 answers

, , HMACWithAlogorithm: , , , UTF8 . , HMACWithAlgorithm: CCHmac CommmonCrypto .

, , , NSString :

NSData *hmac = [aData HMACWithAlgorithm:kCCHmacAlgSHA1];

// Get a pointer to the raw bytes of the digest
unsigned char *digest = (unsigned char *)[hmac bytes];

// Convert the bytes to their hex representation
NSString *hmacStr = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7],
                digest[8], digest[9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15],
                digest[16], digest[17], digest[18], digest[19]];      


NSLog(@"Hashed Data=%@",hmacStr);
+4

(hmacStr1) . NULL, .

, , :

NSLog(@"about to hash %@, which is %d bytes long", aData, [aData length]);
NSData *hmac = [aData HMACWithAlgorithm:kCCHmacAlgSHA1];
NSLog(@"the hash result is %@, which is %d bytes long", hmac, [hmac length]);

, ? .

0

All Articles