Expo

Documentation

Crypto

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

For managed apps, you'll need to run expo install expo-crypto. To use it in a bare React Native app, follow its installation instructions.

import * as Crypto from 'expo-crypto';

digestStringAsync(
  algorithm: CryptoDigestAlgorithm,
  data: string,
  options: CryptoDigestOptions = { encoding: CryptoEncoding.HEX }
): Promise
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.
🍎 iOS💚 Android💻 Web
Parameters
NameTypeDescription
algorithmCryptoDigestAlgorithmTransforms a value into a fixed-size hash (usually shorter than the initial value).
datastringThe value that will be used to generate a digest.
optionsCryptoDigestOptionsFormat of the digest string. Defaults to: CryptoDigestOptions.HEX
Returns
NameTypeDescription
digestPromise<string>Resolves into a value representing the hashed input.
Example
const digest = await Crypto.digestStringAsync(
  Crypto.CryptoDigestAlgorithm.SHA512,
  '🥓 Easy to Digest! 💙'
);

Cryptographic hash function is an algorithm that can be used to generate a checksum value. They have a variety of applications in cryptography.
Cryptographic hash functions like SHA1, MD5 are vulnerable! Attacks have been proven to significantly reduce their collision resistance.
NameTypeDescriptionCollision Resistant🍎 iOS💚 Android💻 Web
SHA1string160 bits
SHA256string256 bits
SHA384string384 bits
SHA512string512 bits
MD2string128 bits
MD4string128 bits
MD5string128 bits

NameType🍎 iOS💚 Android💻 Web
HEX'hex'
BASE64'base64'
Base64 Format
  • Has trailing padding.
  • Does not wrap lines.
  • Does not have a trailing newline.

NameTypeDescription🍎 iOS💚 Android💻 Web
encodingCryptoEncodingFormat the digest is returned in

Usage

import React from 'react';
import { View } from 'react-native';
import * as Crypto from 'expo-crypto';

export default class DemoView extends React.Component {
  async componentDidMount() {
    const digest = await Crypto.digestStringAsync(
      Crypto.CryptoDigestAlgorithm.SHA256,
      'Github stars are neat 🌟'
    );
    console.log('Digest: ', digest);

    /* Some crypto operation... */
  }
  render() {
    return ;
  }
}