+ (NSString *)generateRandom {
static int size = 8;
uint8_t randomBytes[size];
int result = SecRandomCopyBytes(kSecRandomDefault, size, randomBytes);
if (result == errSecSuccess) {
NSMutableString *randomString = [[NSMutableString alloc] initWithCapacity:size * 2];
for (int i = 0; i < size; i++) {
[randomString appendFormat:@"%02x", randomBytes[i]];
}
return randomString;
} else {
return nil;
}
}
这里我们生成一个 8 字节长的 uint8_t 数组,然后将其转换成 hex 字符串得到一个长度16的随机字条串。另外这个函数也可以作为生成 UUID 的辅助操作,如下代码所示:
+ (NSString*)generateCryptoSecureUUID
{
unsigned char bytes[16];
int result = SecRandomCopyBytes(kSecRandomDefault, 16, bytes);
if (result != noErr) {
return nil;
}
return [[NSUUID alloc] initWithUUIDBytes:bytes].UUIDString;
}