Cryptographically Secure Random Bytes for React Native
The `react-native-securerandom` library provides a robust solution for generating cryptographically-secure random bytes within React Native applications. Currently stable at version `1.0.1`, it utilizes native platform APIs such as `SecRandomCopyBytes` on iOS, `SecureRandom` on Android, and `System.Security.Cryptography.RandomNumberGenerator` on Windows to ensure true cryptographic randomness, differentiating it from JavaScript-only PRNGs. Recent updates, including version `1.0.1`, address compatibility with newer React Native versions (e.g., Gradle 7 support for RN 0.68+) and have seen new maintainers join the project, indicating ongoing but generally slower maintenance focused on compatibility and stability rather than rapid feature additions.
Common errors
-
Invariant Violation: Native module RNSecureRandom is not available. Did you forget to link the native module?
cause The native module for `react-native-securerandom` was not correctly linked during the build process, or auto-linking failed.fixEnsure the package is properly linked. If `react-native link` doesn't work, follow the 'Manual linking' instructions in the README for your specific platform (iOS, Android, Windows). -
Execution failed for task ':app:processDebugMainManifest'. > com.android.builder.internal.aapt.AaptException: AAPT2 error: check logs for details
cause A generic Android build error, often related to `react-native-securerandom` not being correctly integrated into the Android project's Gradle files.fixVerify that `android/settings.gradle` and `android/app/build.gradle` have the correct entries for `react-native-securerandom` as specified in the README's manual linking section. -
Cannot find name 'generateSecureRandom'. Did you mean to import it from 'react-native-securerandom'?
cause Incorrect import statement or missing type declaration for `generateSecureRandom` in a TypeScript project.fixUse the named import: `import { generateSecureRandom } from 'react-native-securerandom';` Ensure your TypeScript configuration correctly includes `node_modules` types.
Warnings
- breaking Error handling for improper native module linking changed from synchronously throwing an error to returning a rejected Promise.
- breaking Android devices prior to API 19 (Android < 4.4) using versions `<=0.3.0` may have suffered from insufficient entropy, leading to less secure random number generation.
- gotcha Projects using React Native 0.68+ (which utilizes Gradle 7) may encounter Android build errors due to older Gradle compatibility issues.
- gotcha Automatic linking (`react-native link`) might fail or require extensive manual configuration, especially for iOS Cocoapods or Android project setups, or in managed Expo workflows (which do not support native modules).
- gotcha Older versions (pre-1.0.0) might experience issues with Cocoapods due to the podspec not being in the project root, affecting auto-detection.
Install
-
npm install react-native-securerandom -
yarn add react-native-securerandom -
pnpm add react-native-securerandom
Imports
- generateSecureRandom
const generateSecureRandom = require('react-native-securerandom');import { generateSecureRandom } from 'react-native-securerandom'; - generateSecureRandom (Promise usage)
import { generateSecureRandom } from 'react-native-securerandom'; const randomBytes = generateSecureRandom(16); // Missing awaitimport { generateSecureRandom } from 'react-native-securerandom'; await generateSecureRandom(16); - TypeScript types
import { generateSecureRandom, type SecureRandomError } from 'react-native-securerandom';
Quickstart
import { generateSecureRandom } from 'react-native-securerandom';
async function getRandomBytes(length: number): Promise<Uint8Array> {
try {
const randomBytes = await generateSecureRandom(length);
console.log(`Generated ${length} secure random bytes:`, randomBytes);
return randomBytes;
} catch (error) {
console.error('Failed to generate secure random bytes:', error);
throw error;
}
}
// Example usage:
getRandomBytes(12).then(bytes => console.log('First batch done.'));
getRandomBytes(32).then(bytes => console.log('Second batch done.'));