Skip to content

Instantly share code, notes, and snippets.

@sgl0v
Created November 10, 2015 14:06
Show Gist options
  • Save sgl0v/7014e40249f974538863 to your computer and use it in GitHub Desktop.
Save sgl0v/7014e40249f974538863 to your computer and use it in GitHub Desktop.
Random string generator in ObjC
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
NSString* randomString(NSUInteger length)
{
NSMutableData *data = [NSMutableData dataWithLength:length];
NSInteger result = SecRandomCopyBytes(kSecRandomDefault, length, data.mutableBytes);
NSLog(@"%ld", (long)result);
NSMutableString* str = [NSMutableString string];
for (int i = 0; i < length / 8; i++)
{
UInt8 *bytes = (UInt8*)data.bytes;
const UInt8 ch = bytes[i];
[str appendFormat:@"0x%02x, ", ch];
}
return str;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"%@", randomString(256));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment