This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo Crypto iconExpo Crypto

A universal library for crypto operations.

Android
iOS
tvOS
Web
Included in Expo Go
Recommended version:
~56.0.5

expo-crypto enables you to hash data in an equivalent manner to the Node.js core crypto API, and perform crypto operations such as AES encryption and decryption.

Installation

Terminal
- npx expo install expo-crypto
- yarn expo install expo-crypto
- pnpm expo install expo-crypto
- bun expo install expo-crypto

If you are installing this in an existing React Native app, make sure to install expo in your project.

Usage

Basic Crypto usage
import { useEffect } from 'react'; import { StyleSheet, View, Text } from 'react-native'; import * as Crypto from 'expo-crypto'; export default function App() { useEffect(() => { (async () => { const digest = await Crypto.digestStringAsync( Crypto.CryptoDigestAlgorithm.SHA256, 'GitHub stars are neat 🌟' ); console.log('Digest: ', digest); /* Some crypto operation... */ })(); }, []); return ( <View style={styles.container}> <Text>Crypto Module Example</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });

AES encryption and decryption

Simple encryption / decryption
import { useEffect } from 'react'; import { StyleSheet, View, Text } from 'react-native'; import { AESEncryptionKey, aesEncryptAsync, aesDecryptAsync } from 'expo-crypto'; export default function App() { useEffect(() => { (async () => { const plaintext = 'Hello, world!'; const plaintextBase64 = btoa(plaintext); const encryptionKey = await AESEncryptionKey.generate(); const sealedData = await aesEncryptAsync(plaintextBase64, key); const decryptedBase64 = await aesDecryptAsync(sealedData, key, { output: 'base64', }); const decrypted = atob(decryptedBase64); console.log('Decrypted: ', decrypted); })(); }, []); return ( <View style={styles.container}> <Text>Crypto Module Example</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, });
Encrypt data and save it to the file
example.ts
import { AESEncryptionKey, aesEncryptAsync } from 'expo-crypto'; import { File, Paths } from 'expo-file-system'; import * as SecureStore from 'expo-secure-store'; async function encryptAndSaveData(plaintextData: Uint8Array) { // Generate encryption key const encryptionKey = await AESEncryptionKey.generate(); // Encrypt data const sealedData = await aesEncryptAsync(plaintextData, encryptionKey); const encryptedBytes = await sealedData.combined(); // Store encryption key const keyHex = await encryptionKey.encoded('hex'); await SecureStore.setItemAsync('aes-encryption-key', keyHex); // Save encrypted file const file = new File(Paths.cache, 'encrypted.dat'); file.create({ overwrite: true }); file.write(encryptedBytes); }
Load file and decrypt data
example.ts
import { AESEncryptionKey, AESSealedData, aesDecryptAsync } from 'expo-crypto'; import { File, Paths } from 'expo-file-system'; import * as SecureStore from 'expo-secure-store'; async function loadAndDecryptData(): Promise<Uint8Array | null> { // Load encryption key const keyHex = await SecureStore.getItemAsync('aes-encryption-key'); if (!keyHex) { return null; } const encryptionKey = await AESEncryptionKey.import(keyHex, 'hex'); // Load encrypted file const file = new File(Paths.cache, 'encrypted.dat'); if (!file.exists) { return null; } const encryptedBytes = await file.bytes(); const sealedData = AESSealedData.fromCombined(encryptedBytes); // Decrypt data const plaintextBytes = await aesDecryptAsync(data, encryptionKey); return plaintextBytes; }

API

import * as Crypto from 'expo-crypto';

No API data file found, sorry!

Error codes

CodeDescription
ERR_CRYPTO_UNAVAILABLEWeb Only. Access to the WebCrypto API is restricted to secure origins (localhost/https).
ERR_CRYPTO_DIGESTAn invalid encoding type provided.