SRRandom.m 783 B

1234567891011121314151617181920212223242526
  1. //
  2. // Copyright (c) 2016-present, Facebook, Inc.
  3. // All rights reserved.
  4. //
  5. // This source code is licensed under the BSD-style license found in the
  6. // LICENSE file in the root directory of this source tree. An additional grant
  7. // of patent rights can be found in the PATENTS file in the same directory.
  8. //
  9. #import "SRRandom.h"
  10. #import <Security/SecRandom.h>
  11. NS_ASSUME_NONNULL_BEGIN
  12. NSData *SRRandomData(NSUInteger length)
  13. {
  14. NSMutableData *data = [NSMutableData dataWithLength:length];
  15. int result = SecRandomCopyBytes(kSecRandomDefault, data.length, data.mutableBytes);
  16. if (result != 0) {
  17. [NSException raise:NSInternalInconsistencyException format:@"Failed to generate random bytes with OSStatus: %d", result];
  18. }
  19. return data;
  20. }
  21. NS_ASSUME_NONNULL_END