Created
November 10, 2015 14:06
-
-
Save sgl0v/7014e40249f974538863 to your computer and use it in GitHub Desktop.
Random string generator in ObjC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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