Complete Setup Process
Detailed guide for installation and configuration
This guide covers the full installation process, including how to configure RNQC as a drop-in replacement for the Node.js crypto module.
Installation
Install the package and the required react-native-nitro-modules core.
Install Dependencies
bun add react-native-quick-crypto react-native-nitro-modulesFor Expo projects, use the Expo CLI to ensure compatibility.
Install Dependencies
npx expo install react-native-quick-cryptoAdd Config Plugin
Add the library to your plugins list in app.json (or app.config.js).
{
"expo": {
"plugins": [
"react-native-quick-crypto"
]
}
}Prebuild
Generate the native android and ios directories.
npx expo prebuildThis library requires native code. You must run prebuild to generate the native directories. It will not work in Expo Go.
Global Polyfill (Recommended)
If you want crypto and Buffer to be available globally (as they are in Node.js), import the install function as early as possible in your application's entry point (e.g., index.js).
import { install } from 'react-native-quick-crypto';
install();Configuration
Use metro.config.js to redirect imports at the bundler level. This is generally more robust.
Open Config
Open your metro.config.js file.
Add Resolver
Add the resolveRequest handler to redirect crypto imports.
const { getDefaultConfig } = require('@react-native/metro-config');
const config = getDefaultConfig(__dirname);
config.resolver.resolveRequest = (context, moduleName, platform) => {
if (moduleName === 'crypto') {
// when importing crypto, resolve to react-native-quick-crypto
return context.resolveRequest(
context,
'react-native-quick-crypto',
platform,
);
}
// otherwise chain to the standard Metro resolver.
return context.resolveRequest(context, moduleName, platform);
};
module.exports = config;Alternatively, use babel-plugin-module-resolver to alias imports during transpilation.
Install Plugin
yarn add --dev babel-plugin-module-resolverUpdate Babel Config
Add the plugin to your babel.config.js.
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
+ [
+ 'module-resolver',
+ {
+ alias: {
+ 'crypto': 'react-native-quick-crypto',
+ 'stream': 'readable-stream',
+ 'buffer': '@craftzdog/react-native-buffer',
+ },
+ },
+ ],
...
],
};XSalsa20 Support (Optional)
If you need to use the xsalsa20 cipher algorithm, you must enable libsodium support by setting an environment variable before building your native code:
export SODIUM_ENABLED=1This can be done:
- iOS: Export the variable
export SODIUM_ENABLED=1before runningpod install. - Android: Add
sodiumEnabled=trueto your project'sgradle.propertiesfile.
Without this flag, attempting to use xsalsa20 will throw a runtime error: "libsodium must be enabled to use this cipher"