What is expo-crypto?
The expo-crypto package provides cryptographic utilities for Expo and React Native applications. It allows developers to perform various cryptographic operations such as hashing, HMAC, and generating random bytes.
What are expo-crypto's main functionalities?
Hashing
This feature allows you to generate a hash of a given string using various algorithms like SHA-256. The code sample demonstrates how to generate a SHA-256 hash of the string 'Hello, world!'.
const Crypto = require('expo-crypto');
async function generateHash() {
const digest = await Crypto.digestStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256,
'Hello, world!'
);
console.log(digest);
}
generateHash();
HMAC
This feature allows you to generate an HMAC (Hash-based Message Authentication Code) using a specified algorithm and key. The code sample demonstrates how to generate an HMAC using SHA-256 and a secret key.
const Crypto = require('expo-crypto');
async function generateHMAC() {
const hmac = await Crypto.hmacStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256,
'Hello, world!',
'secret-key'
);
console.log(hmac);
}
generateHMAC();
Random Bytes
This feature allows you to generate a specified number of random bytes. The code sample demonstrates how to generate 16 random bytes.
const Crypto = require('expo-crypto');
async function generateRandomBytes() {
const randomBytes = await Crypto.getRandomBytesAsync(16);
console.log(randomBytes);
}
generateRandomBytes();
Other packages similar to expo-crypto
crypto-js
CryptoJS is a widely-used library that provides a variety of cryptographic algorithms including hashing, HMAC, encryption, and decryption. Unlike expo-crypto, CryptoJS is not specifically designed for React Native or Expo, but it can be used in both web and mobile applications.
react-native-crypto
react-native-crypto is a library that provides a native implementation of Node's crypto module for React Native. It offers similar functionalities to expo-crypto, such as hashing and HMAC, but with a broader scope as it aims to be a drop-in replacement for Node's crypto module.
sjcl
Stanford Javascript Crypto Library (SJCL) is a library focused on cryptographic operations in JavaScript. It provides features like hashing, HMAC, and encryption. While it is not specifically designed for React Native or Expo, it can be used in those environments with some adjustments.
expo-crypto
Provides cryptography primitives.
API documentation
Installation in managed Expo projects
For managed managed Expo projects, please follow the installation instructions in the API documentation for the latest stable release.
Installation in bare React Native projects
For bare React Native projects, you must ensure that you have installed and configured the react-native-unimodules
package before continuing.
Add the package to your npm dependencies
expo install expo-crypto
Configure for iOS
Run npx pod-install
after installing the npm package.
Configure for Android
No additional set up necessary.
Contributing
Contributions are very welcome! Please refer to guidelines described in the contributing guide.