Comprehensive crypto polyfills for React Native - provides crypto.getRandomValues, TextEncoder, TextDecoder, and ReadableStream for AWS SDK and other libraries
npm install rn-crypto-polyfill![npm version][npm]
![license]
Comprehensive crypto polyfills for React Native applications. Provides implementations for crypto.getRandomValues, TextEncoder, TextDecoder, and ReadableStream that are required by AWS SDK and other libraries.
- ā
crypto.getRandomValues() - Secure random number generation
- ā
TextEncoder - UTF-8 string encoding
- ā
TextDecoder - UTF-8 string decoding
- ā
ReadableStream - Stream API polyfill
- ā
TypeScript support - Full type definitions included
- ā
Configurable - Customize behavior via configuration
- ā
Lightweight - No native dependencies
- ā
Battle-tested - Used in production apps
``bash`
npm install rn-crypto-polyfill
or
`bash`
yarn add rn-crypto-polyfill
Import the polyfill at the very beginning of your app's entry point (usually index.js):
`javascript
// index.js
import 'rn-crypto-polyfill';
import { AppRegistry } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
`
That's it! All polyfills are now available globally.
You can customize the behavior of the polyfills:
`javascript
import { configure } from 'rn-crypto-polyfill';
configure({
enableLogging: true, // Enable debug logging (default: false)
useSecureRandom: true, // Use more secure random generation (default: true)
});
`
`javascript
import 'rn-crypto-polyfill';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { Sha256 } from '@aws-crypto/sha256-js';
const client = new S3Client({
region: 'us-east-1',
credentials: {
accessKeyId: 'YOUR_ACCESS_KEY',
secretAccessKey: 'YOUR_SECRET_KEY',
},
sha256: Sha256, // Required for React Native
});
// Now you can use S3 operations!
`
`javascript
import 'rn-crypto-polyfill';
import { v4 as uuidv4 } from 'uuid';
const id = uuidv4(); // Works perfectly!
`
`javascript
const encoder = new TextEncoder();
const bytes = encoder.encode('Hello, World!');
console.log(bytes); // Uint8Array
const decoder = new TextDecoder();
const text = decoder.decode(bytes);
console.log(text); // "Hello, World!"
`
- React Native >= 0.60.0
- Node.js >= 14.0.0
Implements the Web Crypto API's getRandomValues() method. Uses a combination of Math.random() with XOR and timestamps for better entropy.
`javascript`
const array = new Uint8Array(16);
crypto.getRandomValues(array);
Converts JavaScript strings to UTF-8 encoded Uint8Array.
`javascript`
const encoder = new TextEncoder();
const encoded = encoder.encode('Hello š');
Converts UTF-8 encoded bytes back to JavaScript strings.
`javascript`
const decoder = new TextDecoder();
const decoded = decoder.decode(uint8Array);
Minimal implementation of the Streams API ReadableStream. Primarily used for AWS SDK type checking.
`javascript`
const stream = new ReadableStream();
This library is compatible with:
- ā
AWS SDK v3 (@aws-sdk/client-s3, etc.)
- ā
uuid
- ā
crypto-js
- ā
Any library requiring crypto.getRandomValues
- ā
Any library requiring TextEncoder/TextDecoder
Configure the polyfill behavior.
Parameters:
- config.enableLogging (boolean): Enable debug console loggingconfig.useSecureRandom
- (boolean): Use enhanced random number generation
Example:
`javascript
import { configure } from 'rn-crypto-polyfill';
configure({
enableLogging: __DEV__,
useSecureRandom: true,
});
`
Make sure the import is at the very top of your entry file, before any other imports that might use crypto APIs.
ā Wrong:
`javascript`
import { S3Client } from '@aws-sdk/client-s3';
import 'rn-crypto-polyfill'; // Too late!
ā
Correct:
`javascript`
import 'rn-crypto-polyfill'; // First!
import { S3Client } from '@aws-sdk/client-s3';
For AWS SDK, make sure to also import the SHA256 implementation:
`javascript
import { Sha256 } from '@aws-crypto/sha256-js';
const client = new S3Client({
// ... other config
sha256: Sha256,
});
``
MIT Ā© Binarywise
Contributions are welcome! Please feel free to submit a Pull Request.
Inspired by the React Native community's need for Web Crypto API compatibility.
- AWS SDK for JavaScript v3
- Web Crypto API
- TextEncoder API