Guides
Plan-enterprise-icon
Expo Application Services
API Reference

Crypto

expo-crypto enables you to hash data in an equivalent manner to the Node.js core crypto API.

Platform Compatibility

Android DeviceAndroid EmulatoriOS DeviceiOS SimulatorWeb
Status-success-iconStatus-success-iconStatus-success-iconStatus-success-iconStatus-success-icon

Installation

Terminal
→ expo install expo-crypto

If you're installing this in a bare React Native app, you should also follow these additional installation instructions.

Basic Crypto usage
import React, { 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>
  );
}

%%placeholder-start%%const styles = StyleSheet.create({ ... }); %%placeholder-end%%const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

import * as Crypto from 'expo-crypto';

NameTypeDescription
algorithmCryptoDigestAlgorithmThe cryptographic hash function to use to transform a block of data into a fixed-size output.
datastringThe value that will be used to generate a digest.
options
(optional)
CryptoDigestOptionsFormat of the digest string. Defaults to: CryptoDigestOptions.HEX.

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.

Example

const digest = await Crypto.digestStringAsync(
  Crypto.CryptoDigestAlgorithm.SHA512,
  '🥓 Easy to Digest! 💙'
);
  • Undo-iconPromise<Digest>

Return a Promise which fulfills with a value representing the hashed input.

NameTypeDescription
encodingCryptoEncodingFormat the digest is returned in.

Type: string

Cryptographic hash function

Only for:
Apple-iconiOS

CryptoDigestAlgorithm.MD2 = "MD2"

128 bits.

Only for:
Apple-iconiOS

CryptoDigestAlgorithm.MD4 = "MD4"

128 bits.

Only for:
Android-iconAndroid
Apple-iconiOS

CryptoDigestAlgorithm.MD5 = "MD5"

128 bits.

CryptoDigestAlgorithm.SHA1 = "SHA-1"

160 bits.

CryptoDigestAlgorithm.SHA256 = "SHA-256"

256 bits. Collision Resistant.

CryptoDigestAlgorithm.SHA384 = "SHA-384"

384 bits. Collision Resistant.

CryptoDigestAlgorithm.SHA512 = "SHA-512"

512 bits. Collision Resistant.

CryptoEncoding.BASE64 = "base64"

Has trailing padding. Does not wrap lines. Does not have a trailing newline.

CryptoEncoding.HEX = "hex"

CodeDescription
ERR_CRYPTO_UNAVAILABLEWeb Only. Access to the WebCrypto API is restricted to secure origins (https).
You can run your web project from a secure origin with expo start --https.
ERR_CRYPTO_DIGESTAn invalid encoding type provided.