Expo 加密
用于加密操作的通用库。
expo-crypto 使你能够以与 Node.js 核心 crypto API 等效的方式对数据进行哈希处理。
¥expo-crypto enables you to hash data in an equivalent manner to the Node.js core crypto API.
安装
¥Installation
- npx expo install expo-cryptoIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
¥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', }, });
API
import * as Crypto from 'expo-crypto';
Methods
| Parameter | Type | Description |
|---|---|---|
| algorithm | CryptoDigestAlgorithm | The cryptographic hash function to use to transform a block of data into a fixed-size output. |
| data | BufferSource | The value that will be used to generate a digest. |
The digest() method of Crypto generates a digest of the supplied TypedArray of bytes data with the provided digest algorithm.
A digest is a short fixed-length value derived from some variable-length input. Cryptographic digests should exhibit collision-resistance,
meaning that it's very difficult to generate multiple inputs that have equal digest values.
On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
Promise<ArrayBuffer>A Promise which fulfills with an ArrayBuffer representing the hashed input.
Example
const array = new Uint8Array([1, 2, 3, 4, 5]); const digest = await Crypto.digest(Crypto.CryptoDigestAlgorithm.SHA512, array); console.log('Your digest: ' + digest);
| Parameter | Type | Description |
|---|---|---|
| algorithm | CryptoDigestAlgorithm | The cryptographic hash function to use to transform a block of data into a fixed-size output. |
| data | string | The value that will be used to generate a digest. |
| options(optional) | CryptoDigestOptions | Format of the digest string. Defaults to: |
The digestStringAsync() method of Crypto generates a digest of the supplied data string with the provided digest algorithm.
A digest is a short fixed-length value derived from some variable-length input. Cryptographic digests should exhibit collision-resistance,
meaning that it's very difficult to generate multiple inputs that have equal digest values.
You can specify the returned string format as one of CryptoEncoding. By default, the resolved value will be formatted as a HEX string.
On web, this method can only be called from a secure origin (HTTPS) otherwise, an error will be thrown.
Return a Promise which fulfills with a value representing the hashed input.
Example
const digest = await Crypto.digestStringAsync( Crypto.CryptoDigestAlgorithm.SHA512, '🥓 Easy to Digest! 💙' );
| Parameter | Type | Description |
|---|---|---|
| byteCount | number | A number within the range from |
Generates completely random bytes using native implementations. The byteCount property
is a number indicating the number of bytes to generate in the form of a Uint8Array.
Falls back to Math.random during development to prevent issues with React Native Debugger.
Uint8ArrayAn array of random bytes with the same length as the byteCount.
| Parameter | Type | Description |
|---|---|---|
| byteCount | number | A number within the range from |
Generates completely random bytes using native implementations. The byteCount property
is a number indicating the number of bytes to generate in the form of a Uint8Array.
Promise<Uint8Array>A promise that fulfills with an array of random bytes with the same length as the byteCount.
| Parameter | Type | Description |
|---|---|---|
| typedArray | T | An integer based |
The getRandomValues() method of Crypto fills a provided TypedArray with cryptographically secure random values.
TThe input array filled with cryptographically secure random values.
Example
const byteArray = new Uint8Array(16); Crypto.getRandomValues(byteArray); console.log('Your lucky bytes: ' + byteArray);
The randomUUID() method returns a unique identifier based on the V4 UUID spec (RFC4122).
It uses cryptographically secure random values to generate the UUID.
stringA string containing a newly generated UUIDv4 identifier
Example
const UUID = Crypto.randomUUID(); console.log('Your UUID: ' + UUID);
Types
| Property | Type | Description |
|---|---|---|
| encoding | CryptoEncoding | Format the digest is returned in. |
Enums
CryptoEncoding.BASE64 = "base64"Has trailing padding. Does not wrap lines. Does not have a trailing newline.
错误代码
¥Error codes
| 代码 | 描述 |
|---|---|
ERR_CRYPTO_UNAVAILABLE | 仅限网络。对 WebCrypto API 的访问仅限于安全来源 (localhost/https)。 |
ERR_CRYPTO_DIGEST | 提供的编码类型无效。 |