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
A universal library for crypto operations.
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
If you are installing this in an existing React Native app, make sure to install expo in your project.
Usage
AES encryption and decryption
API
import * as Crypto from 'expo-crypto';
Classes
Type: Class extends EncryptionKey
Represents an AES encryption key that can be used for encryption and decryption operations. This class provides methods to generate, import, and export encryption keys.
AESEncryptionKey Properties
AESEncryptionKey Methods
Retrieves the key as a byte array.
Asynchronous due to the use of SubtleCrypto exportKey API.
Promise<Uint8Array<ArrayBufferLike>>A promise that resolves to the byte array representation of the key.
Retrieves the key encoded as a string in the specified format.
Asynchronous due to the use of SubtleCrypto exportKey API.
Promise<string>A promise that resolves to the string representation of the key.
Generates a new AES encryption key of the specified size.
Promise<EncryptionKey>A promise that resolves to an EncryptionKey instance.
Imports an encryption key from a byte array. Validates the size of the key.
Promise<EncryptionKey>A promise that resolves to an EncryptionKey instance.
Imports an encryption key from a string representation (hex or base64). Validates the size of the key.
Promise<EncryptionKey>A promise that resolves to an EncryptionKey instance.
Type: Class extends SealedData
Represents encrypted data including the ciphertext, initialization vector, and authentication tag. This class provides methods to create sealed data from various formats and extract its components.
AESSealedData Properties
numberTotal size of the combined data (IV + ciphertext + tag) in bytes.
AESSealedData Methods
Retrieves the ciphertext from the sealed data.
Promise<string | Uint8Array<ArrayBufferLike>>The ciphertext as a Uint8Array or base64 string depending on encoding option.
Retrieves a combined representation of the IV, ciphertext, and tag.
Promise<string | Uint8Array<ArrayBufferLike>>The combined data as a Uint8Array or base64 string depending on encoding.
Static method. Creates a SealedData instance from a combined byte array, including the IV, ciphertext, and tag.
AESSealedDataA SealedData object.
Static method. Creates a SealedData instance from separate nonce, ciphertext, and optionally a tag.
AESSealedDataA SealedData object.
Static method. Creates a SealedData instance from separate nonce, ciphertext, and optionally a tag.
AESSealedDataA SealedData object.
Retrieves the initialization vector (nonce) from the sealed data.
Promise<string | Uint8Array<ArrayBufferLike>>The initialization vector as a Uint8Array or base64 string depending on encoding.
Retrieves the authentication tag from the sealed data.
Promise<string | Uint8Array<ArrayBufferLike>>The authentication tag as a Uint8Array or base64 string depending on encoding.
Methods
Decrypts the given sealed data using the specified key and options.
Promise<string | Uint8Array<ArrayBufferLike>>A promise that resolves to the decrypted data buffer or string, depending on encoding option.
Encrypts the given plaintext using AES-GCM with the specified key.
Promise<AESSealedData>A promise that resolves to a SealedData instance containing the encrypted data.
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);
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.
Promise<string>Return a Promise which fulfills with a value representing the hashed input.
Example
const digest = await Crypto.digestStringAsync( Crypto.CryptoDigestAlgorithm.SHA512, '🥓 Easy to Digest! 💙' );
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.
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<ArrayBufferLike>>A promise that fulfills with an array of random bytes with the same length as the byteCount.
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);
Interfaces
Types
Literal type: union
Represents binary input data that can be processed by AES APIs. When providing a string, it must be base64-encoded.
Acceptable values are: string | Uint8Array | ArrayBuffer
Configuration for the nonce (initialization vector) during encryption. Can specify either the byte length of the IV to generate or provide an IV directly.
Type: object shaped as below:
Or object shaped as below:
Literal type: number
Byte length of the GCM authentication tag, is a security parameter. The AES-GCM specification recommends that it should be 16, 15, 14, 13, or 12 bytes, although 8 or 4 bytes may be acceptable in some applications. For additional guidance, see Appendix C of the NIST Publication on "Recommendation for Block Cipher Modes of Operation".
Default and recommended value is 16. On Apple, the only supported value for encryption is 16.
Acceptable values are: '16' | '15' | '14' | '13' | '12' | '8' | '4'
Enums
AES key sizes in bits.
CryptoEncoding.BASE64 = "base64"Has trailing padding. Does not wrap lines. Does not have a trailing newline.