├── .gitattributes ├── .gitignore ├── .npmrc ├── LICENSE ├── android ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── org │ └── linusu │ ├── RNGetRandomValuesModule.java │ └── RNGetRandomValuesPackage.java ├── index.js ├── index.web.js ├── ios ├── RNGetRandomValues.h ├── RNGetRandomValues.m ├── RNGetRandomValues.xcodeproj │ └── project.pbxproj └── RNGetRandomValues.xcworkspace │ └── contents.xcworkspacedata ├── package.json ├── react-native-get-random-values.podspec └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | *.pbxproj -text -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # OSX 3 | # 4 | .DS_Store 5 | 6 | # node.js 7 | # 8 | node_modules/ 9 | npm-debug.log 10 | yarn-error.log 11 | 12 | 13 | # Xcode 14 | # 15 | build/ 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | xcuserdata 25 | *.xccheckout 26 | *.moved-aside 27 | DerivedData 28 | *.hmap 29 | *.ipa 30 | *.xcuserstate 31 | project.xcworkspace 32 | 33 | 34 | # Android/IntelliJ 35 | # 36 | build/ 37 | .idea 38 | .gradle 39 | local.properties 40 | *.iml 41 | 42 | # BUCK 43 | buck-out/ 44 | \.buckd/ 45 | *.keystore 46 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018, 2020 Linus Unnebäck 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /android/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | // The Android Gradle plugin is only required when opening the android folder stand-alone. 3 | // This avoids unnecessary downloads and potential conflicts when the library is included as a 4 | // module dependency in an application project. 5 | if (project == rootProject) { 6 | repositories { 7 | mavenCentral() 8 | google() 9 | } 10 | def buildGradleVersion = ext.has('buildGradlePluginVersion') ? ext.get('buildGradlePluginVersion') : '4.2.2' 11 | 12 | dependencies { 13 | classpath "com.android.tools.build:gradle:$buildGradleVersion" 14 | } 15 | } 16 | } 17 | 18 | apply plugin: 'com.android.library' 19 | 20 | def safeExtGet(prop, fallback) { 21 | rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback 22 | } 23 | 24 | android { 25 | def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0].toInteger() 26 | if (agpVersion >= 7) { 27 | namespace 'org.linusu' 28 | } 29 | compileSdkVersion safeExtGet('compileSdkVersion', 30) 30 | buildToolsVersion safeExtGet('buildToolsVersion', '30.0.3') 31 | 32 | defaultConfig { 33 | minSdkVersion safeExtGet('minSdkVersion', 16) 34 | targetSdkVersion safeExtGet('targetSdkVersion', 30) 35 | versionCode 1 36 | versionName "1.0" 37 | } 38 | lintOptions { 39 | abortOnError false 40 | } 41 | } 42 | 43 | repositories { 44 | mavenCentral() 45 | google() 46 | maven { 47 | // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm 48 | url "$rootDir/../node_modules/react-native/android" 49 | } 50 | } 51 | 52 | dependencies { 53 | implementation 'com.facebook.react:react-native:+' 54 | } 55 | -------------------------------------------------------------------------------- /android/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /android/src/main/java/org/linusu/RNGetRandomValuesModule.java: -------------------------------------------------------------------------------- 1 | package org.linusu; 2 | 3 | import java.security.NoSuchAlgorithmException; 4 | import java.security.SecureRandom; 5 | 6 | import android.util.Base64; 7 | 8 | import com.facebook.react.bridge.ReactApplicationContext; 9 | import com.facebook.react.bridge.ReactContextBaseJavaModule; 10 | import com.facebook.react.bridge.ReactMethod; 11 | import com.facebook.react.bridge.Callback; 12 | 13 | public class RNGetRandomValuesModule extends ReactContextBaseJavaModule { 14 | 15 | private final ReactApplicationContext reactContext; 16 | 17 | public RNGetRandomValuesModule(ReactApplicationContext reactContext) { 18 | super(reactContext); 19 | this.reactContext = reactContext; 20 | } 21 | 22 | @Override 23 | public String getName() { 24 | return "RNGetRandomValues"; 25 | } 26 | 27 | @ReactMethod(isBlockingSynchronousMethod = true) 28 | public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException { 29 | byte[] data = new byte[byteLength]; 30 | SecureRandom random = new SecureRandom(); 31 | 32 | random.nextBytes(data); 33 | 34 | return Base64.encodeToString(data, Base64.NO_WRAP); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /android/src/main/java/org/linusu/RNGetRandomValuesPackage.java: -------------------------------------------------------------------------------- 1 | package org.linusu; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import com.facebook.react.ReactPackage; 8 | import com.facebook.react.bridge.NativeModule; 9 | import com.facebook.react.bridge.ReactApplicationContext; 10 | import com.facebook.react.uimanager.ViewManager; 11 | import com.facebook.react.bridge.JavaScriptModule; 12 | 13 | public class RNGetRandomValuesPackage implements ReactPackage { 14 | @Override 15 | public List createNativeModules(ReactApplicationContext reactContext) { 16 | return Arrays.asList(new RNGetRandomValuesModule(reactContext)); 17 | } 18 | 19 | // Deprecated from RN 0.47 20 | public List> createJSModules() { 21 | return Collections.emptyList(); 22 | } 23 | 24 | @Override 25 | public List createViewManagers(ReactApplicationContext reactContext) { 26 | return Collections.emptyList(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const base64Decode = require('fast-base64-decode') 2 | const { NativeModules } = require('react-native') 3 | 4 | class TypeMismatchError extends Error {} 5 | class QuotaExceededError extends Error {} 6 | 7 | let warned = false 8 | function insecureRandomValues (array) { 9 | if (!warned) { 10 | console.warn('Using an insecure random number generator, this should only happen when running in a debugger without support for crypto.getRandomValues') 11 | warned = true 12 | } 13 | 14 | for (let i = 0, r; i < array.length; i++) { 15 | if ((i & 0x03) === 0) r = Math.random() * 0x100000000 16 | array[i] = (r >>> ((i & 0x03) << 3)) & 0xff 17 | } 18 | 19 | return array 20 | } 21 | 22 | /** 23 | * @param {number} byteLength 24 | * @returns {string} 25 | */ 26 | function getRandomBase64 (byteLength) { 27 | if (NativeModules.RNGetRandomValues) { 28 | return NativeModules.RNGetRandomValues.getRandomBase64(byteLength) 29 | } else if (NativeModules.ExpoRandom) { 30 | // Expo SDK 41-44 31 | return NativeModules.ExpoRandom.getRandomBase64String(byteLength) 32 | } else if (global.ExpoModules) { 33 | // Expo SDK 45+ 34 | return global.ExpoModules.ExpoRandom.getRandomBase64String(byteLength); 35 | } else { 36 | throw new Error('Native module not found') 37 | } 38 | } 39 | 40 | /** 41 | * @param {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array|Uint8ClampedArray} array 42 | */ 43 | function getRandomValues (array) { 44 | if (!(array instanceof Int8Array || array instanceof Uint8Array || array instanceof Int16Array || array instanceof Uint16Array || array instanceof Int32Array || array instanceof Uint32Array || array instanceof Uint8ClampedArray)) { 45 | throw new TypeMismatchError('Expected an integer array') 46 | } 47 | 48 | if (array.byteLength > 65536) { 49 | throw new QuotaExceededError('Can only request a maximum of 65536 bytes') 50 | } 51 | 52 | // Expo SDK 48+ 53 | if (global.expo && global.expo.modules && global.expo.modules.ExpoCrypto && global.expo.modules.ExpoCrypto.getRandomValues) { 54 | // ExpoCrypto.getRandomValues doesn't return the array 55 | global.expo.modules.ExpoCrypto.getRandomValues(array) 56 | return array 57 | } 58 | 59 | // Calling getRandomBase64 in remote debugging mode leads to the error 60 | // "Calling synchronous methods on native modules is not supported in Chrome". 61 | // So in that specific case we fall back to just using Math.random(). 62 | if (isRemoteDebuggingInChrome()) { 63 | return insecureRandomValues(array) 64 | } 65 | 66 | base64Decode(getRandomBase64(array.byteLength), new Uint8Array(array.buffer, array.byteOffset, array.byteLength)) 67 | 68 | return array 69 | } 70 | 71 | function isRemoteDebuggingInChrome () { 72 | // Remote debugging in Chrome is not supported in bridgeless 73 | if ('RN$Bridgeless' in global && RN$Bridgeless === true) { 74 | return false 75 | } 76 | 77 | return __DEV__ && typeof global.nativeCallSyncHook === 'undefined' 78 | } 79 | 80 | if (typeof global.crypto !== 'object') { 81 | global.crypto = {} 82 | } 83 | 84 | if (typeof global.crypto.getRandomValues !== 'function') { 85 | global.crypto.getRandomValues = getRandomValues 86 | } 87 | -------------------------------------------------------------------------------- /index.web.js: -------------------------------------------------------------------------------- 1 | // The web platform already has support for getRandomValues 2 | -------------------------------------------------------------------------------- /ios/RNGetRandomValues.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @interface RNGetRandomValues : NSObject 4 | -(NSString*)getRandomBase64:(NSUInteger)byteLength; 5 | @end 6 | -------------------------------------------------------------------------------- /ios/RNGetRandomValues.m: -------------------------------------------------------------------------------- 1 | #import "RNGetRandomValues.h" 2 | 3 | @implementation RNGetRandomValues 4 | 5 | - (dispatch_queue_t)methodQueue 6 | { 7 | return dispatch_get_main_queue(); 8 | } 9 | RCT_EXPORT_MODULE() 10 | 11 | RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString*, getRandomBase64:(NSUInteger)byteLength) { 12 | NSMutableData *data = [NSMutableData dataWithLength:byteLength]; 13 | int result = SecRandomCopyBytes(kSecRandomDefault, byteLength, data.mutableBytes); 14 | if (result != errSecSuccess) { 15 | @throw([NSException exceptionWithName:@"NO_RANDOM_BYTES" reason:@"Failed to aquire secure random bytes" userInfo:nil]); 16 | } 17 | return [data base64EncodedStringWithOptions:0]; 18 | } 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /ios/RNGetRandomValues.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | B3E7B58A1CC2AC0600A0062D /* RNGetRandomValues.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNGetRandomValues.m */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXCopyFilesBuildPhase section */ 14 | 58B511D91A9E6C8500147676 /* CopyFiles */ = { 15 | isa = PBXCopyFilesBuildPhase; 16 | buildActionMask = 2147483647; 17 | dstPath = "include/$(PRODUCT_NAME)"; 18 | dstSubfolderSpec = 16; 19 | files = ( 20 | ); 21 | runOnlyForDeploymentPostprocessing = 0; 22 | }; 23 | /* End PBXCopyFilesBuildPhase section */ 24 | 25 | /* Begin PBXFileReference section */ 26 | 134814201AA4EA6300B7C361 /* libRNGetRandomValues.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNGetRandomValues.a; sourceTree = BUILT_PRODUCTS_DIR; }; 27 | B3E7B5881CC2AC0600A0062D /* RNGetRandomValues.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNGetRandomValues.h; sourceTree = ""; }; 28 | B3E7B5891CC2AC0600A0062D /* RNGetRandomValues.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNGetRandomValues.m; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | 58B511D81A9E6C8500147676 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | 134814211AA4EA7D00B7C361 /* Products */ = { 43 | isa = PBXGroup; 44 | children = ( 45 | 134814201AA4EA6300B7C361 /* libRNGetRandomValues.a */, 46 | ); 47 | name = Products; 48 | sourceTree = ""; 49 | }; 50 | 58B511D21A9E6C8500147676 = { 51 | isa = PBXGroup; 52 | children = ( 53 | B3E7B5881CC2AC0600A0062D /* RNGetRandomValues.h */, 54 | B3E7B5891CC2AC0600A0062D /* RNGetRandomValues.m */, 55 | 134814211AA4EA7D00B7C361 /* Products */, 56 | ); 57 | sourceTree = ""; 58 | }; 59 | /* End PBXGroup section */ 60 | 61 | /* Begin PBXNativeTarget section */ 62 | 58B511DA1A9E6C8500147676 /* RNGetRandomValues */ = { 63 | isa = PBXNativeTarget; 64 | buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNGetRandomValues" */; 65 | buildPhases = ( 66 | 58B511D71A9E6C8500147676 /* Sources */, 67 | 58B511D81A9E6C8500147676 /* Frameworks */, 68 | 58B511D91A9E6C8500147676 /* CopyFiles */, 69 | ); 70 | buildRules = ( 71 | ); 72 | dependencies = ( 73 | ); 74 | name = RNGetRandomValues; 75 | productName = RCTDataManager; 76 | productReference = 134814201AA4EA6300B7C361 /* libRNGetRandomValues.a */; 77 | productType = "com.apple.product-type.library.static"; 78 | }; 79 | /* End PBXNativeTarget section */ 80 | 81 | /* Begin PBXProject section */ 82 | 58B511D31A9E6C8500147676 /* Project object */ = { 83 | isa = PBXProject; 84 | attributes = { 85 | LastUpgradeCheck = 0830; 86 | ORGANIZATIONNAME = Facebook; 87 | TargetAttributes = { 88 | 58B511DA1A9E6C8500147676 = { 89 | CreatedOnToolsVersion = 6.1.1; 90 | }; 91 | }; 92 | }; 93 | buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNGetRandomValues" */; 94 | compatibilityVersion = "Xcode 3.2"; 95 | developmentRegion = English; 96 | hasScannedForEncodings = 0; 97 | knownRegions = ( 98 | en, 99 | ); 100 | mainGroup = 58B511D21A9E6C8500147676; 101 | productRefGroup = 58B511D21A9E6C8500147676; 102 | projectDirPath = ""; 103 | projectRoot = ""; 104 | targets = ( 105 | 58B511DA1A9E6C8500147676 /* RNGetRandomValues */, 106 | ); 107 | }; 108 | /* End PBXProject section */ 109 | 110 | /* Begin PBXSourcesBuildPhase section */ 111 | 58B511D71A9E6C8500147676 /* Sources */ = { 112 | isa = PBXSourcesBuildPhase; 113 | buildActionMask = 2147483647; 114 | files = ( 115 | B3E7B58A1CC2AC0600A0062D /* RNGetRandomValues.m in Sources */, 116 | ); 117 | runOnlyForDeploymentPostprocessing = 0; 118 | }; 119 | /* End PBXSourcesBuildPhase section */ 120 | 121 | /* Begin XCBuildConfiguration section */ 122 | 58B511ED1A9E6C8500147676 /* Debug */ = { 123 | isa = XCBuildConfiguration; 124 | buildSettings = { 125 | ALWAYS_SEARCH_USER_PATHS = NO; 126 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 127 | CLANG_CXX_LIBRARY = "libc++"; 128 | CLANG_ENABLE_MODULES = YES; 129 | CLANG_ENABLE_OBJC_ARC = YES; 130 | CLANG_WARN_BOOL_CONVERSION = YES; 131 | CLANG_WARN_CONSTANT_CONVERSION = YES; 132 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 133 | CLANG_WARN_EMPTY_BODY = YES; 134 | CLANG_WARN_ENUM_CONVERSION = YES; 135 | CLANG_WARN_INFINITE_RECURSION = YES; 136 | CLANG_WARN_INT_CONVERSION = YES; 137 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 138 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 139 | CLANG_WARN_UNREACHABLE_CODE = YES; 140 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 141 | COPY_PHASE_STRIP = NO; 142 | ENABLE_STRICT_OBJC_MSGSEND = YES; 143 | ENABLE_TESTABILITY = YES; 144 | GCC_C_LANGUAGE_STANDARD = gnu99; 145 | GCC_DYNAMIC_NO_PIC = NO; 146 | GCC_NO_COMMON_BLOCKS = YES; 147 | GCC_OPTIMIZATION_LEVEL = 0; 148 | GCC_PREPROCESSOR_DEFINITIONS = ( 149 | "DEBUG=1", 150 | "$(inherited)", 151 | ); 152 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 153 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 154 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 155 | GCC_WARN_UNDECLARED_SELECTOR = YES; 156 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 157 | GCC_WARN_UNUSED_FUNCTION = YES; 158 | GCC_WARN_UNUSED_VARIABLE = YES; 159 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 160 | MTL_ENABLE_DEBUG_INFO = YES; 161 | ONLY_ACTIVE_ARCH = YES; 162 | SDKROOT = iphoneos; 163 | }; 164 | name = Debug; 165 | }; 166 | 58B511EE1A9E6C8500147676 /* Release */ = { 167 | isa = XCBuildConfiguration; 168 | buildSettings = { 169 | ALWAYS_SEARCH_USER_PATHS = NO; 170 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 171 | CLANG_CXX_LIBRARY = "libc++"; 172 | CLANG_ENABLE_MODULES = YES; 173 | CLANG_ENABLE_OBJC_ARC = YES; 174 | CLANG_WARN_BOOL_CONVERSION = YES; 175 | CLANG_WARN_CONSTANT_CONVERSION = YES; 176 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 177 | CLANG_WARN_EMPTY_BODY = YES; 178 | CLANG_WARN_ENUM_CONVERSION = YES; 179 | CLANG_WARN_INFINITE_RECURSION = YES; 180 | CLANG_WARN_INT_CONVERSION = YES; 181 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 182 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 183 | CLANG_WARN_UNREACHABLE_CODE = YES; 184 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 185 | COPY_PHASE_STRIP = YES; 186 | ENABLE_NS_ASSERTIONS = NO; 187 | ENABLE_STRICT_OBJC_MSGSEND = YES; 188 | GCC_C_LANGUAGE_STANDARD = gnu99; 189 | GCC_NO_COMMON_BLOCKS = YES; 190 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 191 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 192 | GCC_WARN_UNDECLARED_SELECTOR = YES; 193 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 194 | GCC_WARN_UNUSED_FUNCTION = YES; 195 | GCC_WARN_UNUSED_VARIABLE = YES; 196 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 197 | MTL_ENABLE_DEBUG_INFO = NO; 198 | SDKROOT = iphoneos; 199 | VALIDATE_PRODUCT = YES; 200 | }; 201 | name = Release; 202 | }; 203 | 58B511F01A9E6C8500147676 /* Debug */ = { 204 | isa = XCBuildConfiguration; 205 | buildSettings = { 206 | HEADER_SEARCH_PATHS = ( 207 | "$(inherited)", 208 | /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, 209 | "$(SRCROOT)/../../../React/**", 210 | "$(SRCROOT)/../../react-native/React/**", 211 | ); 212 | LIBRARY_SEARCH_PATHS = "$(inherited)"; 213 | OTHER_LDFLAGS = "-ObjC"; 214 | PRODUCT_NAME = RNGetRandomValues; 215 | SKIP_INSTALL = YES; 216 | }; 217 | name = Debug; 218 | }; 219 | 58B511F11A9E6C8500147676 /* Release */ = { 220 | isa = XCBuildConfiguration; 221 | buildSettings = { 222 | HEADER_SEARCH_PATHS = ( 223 | "$(inherited)", 224 | /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, 225 | "$(SRCROOT)/../../../React/**", 226 | "$(SRCROOT)/../../react-native/React/**", 227 | ); 228 | LIBRARY_SEARCH_PATHS = "$(inherited)"; 229 | OTHER_LDFLAGS = "-ObjC"; 230 | PRODUCT_NAME = RNGetRandomValues; 231 | SKIP_INSTALL = YES; 232 | }; 233 | name = Release; 234 | }; 235 | /* End XCBuildConfiguration section */ 236 | 237 | /* Begin XCConfigurationList section */ 238 | 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RNGetRandomValues" */ = { 239 | isa = XCConfigurationList; 240 | buildConfigurations = ( 241 | 58B511ED1A9E6C8500147676 /* Debug */, 242 | 58B511EE1A9E6C8500147676 /* Release */, 243 | ); 244 | defaultConfigurationIsVisible = 0; 245 | defaultConfigurationName = Release; 246 | }; 247 | 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RNGetRandomValues" */ = { 248 | isa = XCConfigurationList; 249 | buildConfigurations = ( 250 | 58B511F01A9E6C8500147676 /* Debug */, 251 | 58B511F11A9E6C8500147676 /* Release */, 252 | ); 253 | defaultConfigurationIsVisible = 0; 254 | defaultConfigurationName = Release; 255 | }; 256 | /* End XCConfigurationList section */ 257 | }; 258 | rootObject = 58B511D31A9E6C8500147676 /* Project object */; 259 | } 260 | -------------------------------------------------------------------------------- /ios/RNGetRandomValues.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | 3 | 5 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-get-random-values", 3 | "version": "1.11.0", 4 | "license": "MIT", 5 | "repository": "LinusU/react-native-get-random-values", 6 | "dependencies": { 7 | "fast-base64-decode": "^1.0.0" 8 | }, 9 | "peerDependencies": { 10 | "react-native": ">=0.56" 11 | }, 12 | "keywords": [ 13 | "Crypto.getRandomValues", 14 | "crypto", 15 | "get-random-values", 16 | "getRandomValues", 17 | "polyfill", 18 | "react-native" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /react-native-get-random-values.podspec: -------------------------------------------------------------------------------- 1 | require "json" 2 | 3 | package = JSON.parse(File.read(File.join(__dir__, "package.json"))) 4 | 5 | Pod::Spec.new do |s| 6 | s.name = "react-native-get-random-values" 7 | s.version = package["version"] 8 | s.summary = "getRandomValues for React Native" 9 | s.description = "A small implementation of `getRandomValues` for React Native." 10 | s.homepage = "https://github.com/LinusU/react-native-get-random-values" 11 | s.license = "MIT" 12 | s.authors = { "Linus Unnebäck" => "linus@folkdatorn.se" } 13 | s.platforms = { :ios => "9.0", :tvos => "9.0", :osx => "10.14" } 14 | s.source = { :git => "https://github.com/LinusU/react-native-get-random-values.git", :tag => "v#{s.version}" } 15 | 16 | s.source_files = "ios/**/*.{h,m,swift}" 17 | s.requires_arc = true 18 | 19 | s.dependency "React-Core" 20 | end 21 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # `crypto.getRandomValues` for React Native 2 | 3 | A small implementation of `crypto.getRandomValues` for React Native. This is useful to polyfill for libraries like [uuid](https://www.npmjs.com/package/uuid) that depend on it. 4 | 5 | ## Installation 6 | 7 | ```sh 8 | npm install react-native-get-random-values 9 | npx pod-install 10 | ``` 11 | 12 | > 💡 If you use the Expo managed workflow you will see "CocoaPods is not supported in this project" - this is fine, it's not necessary. 13 | 14 | ## Usage 15 | 16 | This library works as a polyfill for the global `crypto.getRandomValues`. 17 | 18 | ```javascript 19 | // Add this line to your `index.js` 20 | import 'react-native-get-random-values' 21 | ``` 22 | 23 | Now you can use `uuid` or other libraries that assume `crypto.getRandomValues` is available. 24 | 25 | ```javascript 26 | import { v4 as uuid } from 'uuid' 27 | 28 | console.log(uuid()) 29 | ``` 30 | 31 | ## API 32 | 33 | ### `crypto.getRandomValues(typedArray)` 34 | 35 | The `crypto.getRandomValues()` method lets you get cryptographically strong random values. The array given as the parameter is filled with random numbers (random in its cryptographic meaning). 36 | 37 | To guarantee enough performance, implementations are not using a truly random number generator, but they are using a pseudo-random number generator *seeded* with a value with enough entropy. The PRNG used differs from one implementation to the other but is suitable for cryptographic usages. Implementations are also required to use a seed with enough entropy, like a system-level entropy source. 38 | 39 | - `typedArray` - Is an integer-based TypedArray, that is an `Int8Array`, a `Uint8Array`, an `Int16Array`, a `Uint16Array`, an `Int32Array`, or a `Uint32Array`. All elements in the array are going to be overridden with random numbers. 40 | 41 | Returns the typed array that was passed in. 42 | --------------------------------------------------------------------------------