Cryptographically Secure Random Bytes for React Native

1.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to import `generateSecureRandom` and asynchronously use it to generate an array of cryptographically secure random bytes, including basic error handling.

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.'));

view raw JSON →