React Native Crypto

2.2.1 · deprecated · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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'));
});

view raw JSON →