React Native Crypto
This package provides a port of Node.js's `crypto` module for React Native environments. It is a direct clone of `crypto-browserify` but replaces `randombytes` with a React Native-compatible implementation. The package is currently at version 2.2.1 and has been explicitly *deprecated* by its maintainers. The recommended alternative is to use `react-native-get-random-values` for secure random byte generation in conjunction with `crypto-browserify` directly. `react-native-crypto`'s primary function was to enable common cryptographic operations such as hashing (SHA, MD5), HMACs, PBKDF2 key derivation, and symmetric encryption/decryption (AES) within React Native applications, which inherently lack Node.js core modules. Its integration required a complex setup involving `rn-nodeify` to shim necessary Node.js modules and an explicit `shim.js` import at the application's entry point to function correctly, making its setup prone to errors. Its release cadence was slow, and maintenance has effectively ceased due to the deprecation notice.
Common errors
-
Unable to resolve module `crypto` from `path/to/your/file.js`
cause The React Native bundler cannot find a definition for the `crypto` module, typically because `rn-nodeify` was not run or the `shim.js` file was not correctly imported.fixFirst, ensure `npm i --save-dev rn-nodeify` and then run `./node_modules/.bin/rn-nodeify --hack --install`. Second, verify that `import './shim.js'` is the *first* line in your main application entry file. -
ReferenceError: Can't find variable: crypto
cause The `crypto` global or imported module is not defined at runtime. This often happens if the `shim.js` is not loaded, or loaded too late, or if `rn-nodeify` failed to correctly configure the shims.fixEnsure `import './shim.js'` is the absolute first statement in your root `index.js` or platform-specific entry files. Double-check that `rn-nodeify` was run with `--hack --install`. -
Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.
cause This error frequently arises from `react-native-randombytes` (a peer dependency of `react-native-crypto`) not being correctly linked as a native module in your React Native project.fixFollow the linking instructions for `react-native-randombytes` carefully. For React Native >= 0.60, this usually involves `cd ios && pod install` after `npm install`. For older versions, use `react-native link react-native-randombytes`.
Warnings
- deprecated This package is officially deprecated by its maintainers. It is strongly recommended to migrate to `react-native-get-random-values` combined with `crypto-browserify` for active maintenance and better compatibility.
- breaking Requires `rn-nodeify` for shimming, which modifies your `package.json` files and creates a `shim.js` entry point. This 'hack' can be intrusive and may cause conflicts with other build tools or React Native upgrades.
- gotcha The `shim.js` file created by `rn-nodeify` *must* be imported as the very first line in your application's entry file (e.g., `index.js` or `index.android.js`/`index.ios.js`). Failure to do so will result in `crypto` or other Node.js core modules being undefined when first accessed.
- gotcha Linking the `react-native-randombytes` peer dependency requires platform-specific steps. For React Native versions >= 0.60, manual `cd iOS && pod install` is needed after `npm install`. For older versions, `react-native link react-native-randombytes` was used.
Install
-
npm install react-native-crypto -
yarn add react-native-crypto -
pnpm add react-native-crypto
Imports
- crypto
const crypto = require('crypto')import crypto from 'crypto'
- createHash
const { createHash } = require('crypto')import { createHash } from 'crypto' - ./shim.js
import './shim.js'
Quickstart
import './shim.js';
import crypto from 'crypto';
// Example: Hashing data
const dataToHash = 'Hello, React Native Crypto!';
const hash = crypto.createHash('sha256');
hash.update(dataToHash);
const hashedData = hash.digest('hex');
console.log('SHA256 Hash:', hashedData);
// Example: Generating random bytes
const randomBytes = crypto.randomBytes(16);
console.log('Random Bytes (hex):', randomBytes.toString('hex'));
// Example: PBKDF2 key derivation
const password = 'mysecretpassword';
const salt = crypto.randomBytes(16);
crypto.pbkdf2(password, salt, 100000, 64, 'sha512', (err, derivedKey) => {
if (err) throw err;
console.log('Derived Key (hex):', derivedKey.toString('hex'));
});