about summary refs log tree commit diff
path: root/ios/AppSecureRandomModule.m
blob: 9aba127fcb5a43e283e5e4af9fbf32ea268f0103 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import "AppSecureRandomModule.h"

@implementation AppSecureRandomModule

RCT_EXPORT_MODULE();

+ (BOOL)requiresMainQueueSetup
{
    return NO;
}

RCT_REMAP_METHOD(generateSecureRandomAsBase64,
                 withLength:(int)length
                 resolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  NSMutableData* bytes = [NSMutableData dataWithLength:length];
  int result = SecRandomCopyBytes(kSecRandomDefault,length, [bytes mutableBytes]);
  if (result == errSecSuccess) {
    resolve([bytes base64EncodedStringWithOptions:0]);
  } else {
    NSError *error = [NSError errorWithDomain:@"RNSecureRandom" code:result userInfo: nil];
    reject(@"randombytes_error", @"Error generating random bytes", error);
  }
}

@end