about summary refs log tree commit diff
path: root/ios/AppSecureRandomModule.m
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2022-06-15 22:08:28 -0500
committerPaul Frazee <pfrazee@gmail.com>2022-06-15 22:08:28 -0500
commita56cae626abf6c553cd9756db875c8ab5f903879 (patch)
tree4c2c8ef9f00006a573959db84c8e3b037105a0bd /ios/AppSecureRandomModule.m
parent2c73703d7d59bdd9a3e4b10c41e5099b8f92db1c (diff)
downloadvoidsky-a56cae626abf6c553cd9756db875c8ab5f903879.tar.zst
Move the secure-random code into this project due to build issues with the module, which is no longer maintained
Diffstat (limited to 'ios/AppSecureRandomModule.m')
-rw-r--r--ios/AppSecureRandomModule.m27
1 files changed, 27 insertions, 0 deletions
diff --git a/ios/AppSecureRandomModule.m b/ios/AppSecureRandomModule.m
new file mode 100644
index 000000000..9aba127fc
--- /dev/null
+++ b/ios/AppSecureRandomModule.m
@@ -0,0 +1,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