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

Expo 加密 iconExpo 加密

用于加密操作的通用库。

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

expo-crypto 使你能够以与 Node.js 核心 crypto API 等效的方式对数据进行哈希,并执行诸如 AES 加密和解密等加密操作。

安装

🌐 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 加密与解密

🌐 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, encryptionKey); const decryptedBase64 = await aesDecryptAsync(sealedData, encryptionKey, { 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', }, });
加密数据并将其保存到文件
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 }); await file.write(encryptedBytes); }
加载文件并解密数据
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

代码描述
ERR_CRYPTO_UNAVAILABLE仅限 Web。 访问 WebCrypto API 仅限于安全来源(localhost/https)。
ERR_CRYPTO_DIGEST提供了无效的编码类型。