Signal-iOS/SignalServiceKit/Randomness/Randomness.m
Harry bb083ca39c
Fold SignalCoreKit into SignalServiceKit
Co-authored-by: Adam Sharp <sharplet@signal.org>
2024-06-26 08:44:41 -07:00

33 lines
786 B
Objective-C

//
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "Randomness.h"
NS_ASSUME_NONNULL_BEGIN
@implementation Randomness
+ (NSData *)generateRandomBytes:(int)numberBytes
{
NSMutableData *_Nullable randomBytes = [NSMutableData dataWithLength:numberBytes];
if (!randomBytes) {
OWSFail(@"Could not allocate buffer for random bytes.");
}
int err = 0;
err = SecRandomCopyBytes(kSecRandomDefault, numberBytes, [randomBytes mutableBytes]);
if (err != noErr || randomBytes.length != numberBytes) {
OWSFail(@"Could not generate random bytes.");
}
NSData *copy = [randomBytes copy];
OWSAssert(copy != nil);
OWSAssert(copy.length == numberBytes);
return copy;
}
@end
NS_ASSUME_NONNULL_END