Registry / auth-security / react-native-crypto

react-native-crypto

JSON →
library2.2.1jsnpmunverified

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.

npm install react-native-crypto
INSTALL
IMPORT
SIG · REACT-NATIVE-CRYPT
R
react-native-crypto
auth-securityjavascriptv2.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

crypto
import crypto from 'crypto'
const crypto = require('crypto')
The shim.js file created by rn-nodeify exports the crypto module as a default export, making a named import difficult without remapping. Requires `./shim.js` to be imported first.
createHash
import { createHash } from 'crypto'
const { createHash } = require('crypto')
While `crypto` itself is a default export, specific methods like `createHash` can be destructured as named imports after the module is globally shimmmed. Requires `./shim.js` to be imported first.
./shim.js
import './shim.js'
This import *must* be the very first import in your application's entry file (e.g., `index.js`, `index.ios.js`, `index.android.js`) to ensure all Node.js core shims are loaded before any other module attempts to use them.

Demonstrates how to set up `react-native-crypto` by importing the `shim.js` and then performing common cryptographic operations like hashing, random byte generation, and PBKDF2 key derivation.

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')); });
Debug
Known issues
deprecatedThis 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.
fix
Migrate your implementation to use `react-native-get-random-values` and `crypto-browserify` instead. Ensure proper polyfills are in place for Buffer if needed.
affects: >=1.0.0
breakingRequires `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.
fix
Carefully follow the `rn-nodeify` installation instructions, ensuring you back up your `package.json` before running `--hack`. Be prepared for potential conflicts during upgrades or if other modules also require similar shimming.
affects: >=1.0.0
gotchaThe `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.
fix
Ensure `import './shim.js'` is the absolute first statement in your main application entry file, before any other imports or code.
affects: >=1.0.0
gotchaLinking 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.
fix
Always refer to the `react-native-randombytes` documentation for the correct linking procedure based on your React Native version. Incorrect linking will lead to runtime errors related to native modules.
affects: >=1.0.0
Errors
Common errors & fixes
Unable to resolve module `crypto` from `path/to/your/file.js`
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.
fix
First, 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
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.
fix
Ensure `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.
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.
fix
Follow 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`.
Upgrade
Version history
2.2.1latest on npm
Audit
Dependencies
react-native-randombytesrequiredPeer dependency for cryptographic-grade random byte generation, essential for many crypto operations.
rn-nodeifyoptionalDevelopment dependency used to shim Node.js core modules (like 'crypto', 'buffer', 'stream') into React Native's bundler by modifying package.json files and creating a shim.js entry.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources