3

I am using CommonCrypto for encryption on Mac OS 10.7. Isn't this framework built in? When I am generating random data:

+ (NSData *)randomDataOfLength:(size_t)length {
NSMutableData *data = [NSMutableData dataWithLength:length];

int result = SecRandomCopyBytes(kSecRandomDefault, 
                                length,
                                data.mutableBytes);
NSAssert(result == 0, @"Unable to generate random bytes: %d",
         errno);

return data;

}

I get the error use of undeclared identifier kSecRandomDefault, which I believe is declared in CommonCrypto.

Thanks, all help is greatly appreciated.

  • It's _installed_ by default, but you have to link against it and import the header. Are you doing that? – jscs Dec 26 '11 at 21:15
  • How would I do that? Do you mean just #import ? –  Dec 26 '11 at 21:17
  • Note that CommonCrypto and the Security framework are two separate things. CommonCrypto is part of libSystem. Moreover, the code you show in your question uses nothing but Cocoa and the Security framework; it contains no CommonCrypto code. – Peter Hosey Dec 27 '11 at 00:43

1 Answers1

11

It's defined in SecRandom.h. Make sure you've included the Security framework in your project and add the appropriate header file. It's not included with the framework default headers (I'm not sure if that's an over site or intentional). So, add the following import to your implementation file:

#import <Security/SecRandom.h>
Jason Coco
  • 77,985
  • 20
  • 184
  • 180