{"id":11766,"library":"react-native-crypto","title":"React Native Crypto","description":"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.","status":"deprecated","version":"2.2.1","language":"javascript","source_language":"en","source_url":"git://github.com/tradle/react-native-crypto","tags":["javascript","react-native","react-component","ios"],"install":[{"cmd":"npm install react-native-crypto","lang":"bash","label":"npm"},{"cmd":"yarn add react-native-crypto","lang":"bash","label":"yarn"},{"cmd":"pnpm add react-native-crypto","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for cryptographic-grade random byte generation, essential for many crypto operations.","package":"react-native-randombytes","optional":false},{"reason":"Development 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.","package":"rn-nodeify","optional":true}],"imports":[{"note":"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.","wrong":"const crypto = require('crypto')","symbol":"crypto","correct":"import crypto from 'crypto'"},{"note":"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.","wrong":"const { createHash } = require('crypto')","symbol":"createHash","correct":"import { createHash } from 'crypto'"},{"note":"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.","symbol":"./shim.js","correct":"import './shim.js'"}],"quickstart":{"code":"import './shim.js';\nimport crypto from 'crypto';\n\n// Example: Hashing data\nconst dataToHash = 'Hello, React Native Crypto!';\nconst hash = crypto.createHash('sha256');\nhash.update(dataToHash);\nconst hashedData = hash.digest('hex');\nconsole.log('SHA256 Hash:', hashedData);\n\n// Example: Generating random bytes\nconst randomBytes = crypto.randomBytes(16);\nconsole.log('Random Bytes (hex):', randomBytes.toString('hex'));\n\n// Example: PBKDF2 key derivation\nconst password = 'mysecretpassword';\nconst salt = crypto.randomBytes(16);\ncrypto.pbkdf2(password, salt, 100000, 64, 'sha512', (err, derivedKey) => {\n  if (err) throw err;\n  console.log('Derived Key (hex):', derivedKey.toString('hex'));\n});","lang":"javascript","description":"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."},"warnings":[{"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.","message":"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.","severity":"deprecated","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure `import './shim.js'` is the absolute first statement in your main application entry file, before any other imports or code.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","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.","error":"Unable to resolve module `crypto` from `path/to/your/file.js`"},{"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`.","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.","error":"ReferenceError: Can't find variable: crypto"},{"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`.","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.","error":"Invariant Violation: `new NativeEventEmitter()` requires a non-null argument."}],"ecosystem":"npm"}