1 | // Copyright 2019-present 650 Industries. All rights reserved.
|
2 |
|
3 | #import <EXRandom/EXRandom.h>
|
4 |
|
5 | @implementation EXRandom
|
6 |
|
7 | UM_EXPORT_MODULE(ExpoRandom);
|
8 |
|
9 | UM_EXPORT_METHOD_AS(getRandomBase64StringAsync,
|
10 | getRandomBase64StringAsync:(NSNumber *)count
|
11 | resolver:(UMPromiseResolveBlock)resolve
|
12 | rejecter:(UMPromiseRejectBlock)reject)
|
13 | {
|
14 | NSUInteger _length = [count unsignedIntegerValue];
|
15 | NSMutableData *bytes = [NSMutableData dataWithLength:_length];
|
16 |
|
17 | OSStatus result = SecRandomCopyBytes(kSecRandomDefault, _length, [bytes mutableBytes]);
|
18 | if (result == errSecSuccess) {
|
19 | resolve([bytes base64EncodedStringWithOptions:0]);
|
20 | } else {
|
21 | reject(@"ERR_RANDOM", @"Failed to create a secure random number", [NSError errorWithDomain:@"expo-random" code:result userInfo:nil]);
|
22 | }
|
23 | }
|
24 |
|
25 |
|
26 | @end
|