Created
April 5, 2021 20:23
-
-
Save eugenehp/9685202ed00b2d0aaf4d403ff583bb2c to your computer and use it in GitHub Desktop.
react-native-randomness Objective-C code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Randomness.h" | |
@implementation Randomness | |
RCT_EXPORT_MODULE() | |
// Example method | |
// See // https://reactnative.dev/docs/native-modules-ios | |
RCT_REMAP_METHOD(random, | |
randomWithLength:(nonnull NSNumber*)length | |
withResolver:(RCTPromiseResolveBlock)resolve | |
withRejecter:(RCTPromiseRejectBlock)reject) | |
{ | |
NSMutableData *data = [NSMutableData dataWithLength:[length integerValue]]; | |
int result = SecRandomCopyBytes(kSecRandomDefault, [length integerValue], data.mutableBytes); | |
if (result != errSecSuccess) { | |
NSError *error = [[NSError alloc] initWithDomain:NSPOSIXErrorDomain | |
code:result userInfo:nil]; | |
reject(@"error", @"Failed to generate random bytes using secure methods.", error); | |
} | |
NSLog(@"Generated random data, %@", data); | |
resolve([data base64EncodedStringWithOptions:0]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment