expo-crypto not returning same hash as node crypto module

Please provide the following:

  1. SDK Version: 45.0.0
  2. Platforms(Android/iOS/web/all): Android/iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

I’m working with the Node crypto library in my web application and expo-crypto in my app. I need to have the same functionality in both. The value from the Node crypto library is correct but for some reason I’m getting another value with expo-crypto.

Currently the web solution is working as expected.

const randomData = 'XTXpQWATVzlHSp2ZjvmoAfvXYDtT3O1q4SYFIDyPXardJhiLbC/tiw==';
const hashedValue = crypto.createHash('sha512').update(randomData).digest('hex');

The hashed value above is 932ed33fb9688dce41620b2a835d353598d410f76fdc0de34b057393443cffcb3733360cf18327df66d8dc120f94dd1c28741f81b098f8dee26c1a0b0a37d4e1

My expo app solution is not returning the same value as the web solution:

import Crypto from 'expo-crypto'
const randomData = 'XTXpQWATVzlHSp2ZjvmoAfvXYDtT3O1q4SYFIDyPXardJhiLbC/tiw==';
const hashedValue = await Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA512, randomData);

This hashed value, from expo-crypto is 270a2e3a3ea41fca841611e2dc539ac6110a9d64cc68cc16165e25020b1c3cec36d3d1680009e999c8f934a2926f0e12be1a1bd6664235cc08da02809028b10c

Ideally they would both return the same value, any ideas on what is going on here? I also posted this to stack overflow

Hi @rikhardur

If I try it in Python or nodejs or OpenSSL or Expo I get the same answer:

$ python3 -c 'import hashlib;print(hashlib.sha512(b"XTXpQWATVzlHSp2ZjvmoAfvXYDtT3O1q4SYFIDyPXardJhiLbC/tiw==").hexdigest())'
270a2e3a3ea41fca841611e2dc539ac6110a9d64cc68cc16165e25020b1c3cec36d3d1680009e999c8f934a2926f0e12be1a1bd6664235cc08da02809028b10c
$ echo -n 'XTXpQWATVzlHSp2ZjvmoAfvXYDtT3O1q4SYFIDyPXardJhiLbC/tiw==' | openssl sha512
SHA512(stdin)= 270a2e3a3ea41fca841611e2dc539ac6110a9d64cc68cc16165e25020b1c3cec36d3d1680009e999c8f934a2926f0e12be1a1bd6664235cc08da02809028b10c
$ node -p 'crypto.createHash("sha512").update("XTXpQWATVzlHSp2ZjvmoAfvXYDtT3O1q4SYFIDyPXardJhiLbC/tiw==").digest("hex");'
270a2e3a3ea41fca841611e2dc539ac6110a9d64cc68cc16165e25020b1c3cec36d3d1680009e999c8f934a2926f0e12be1a1bd6664235cc08da02809028b10c

By the way, that should be:

import * as Crypto from 'expo-crypto';

but I suspect you just had a copy/paste error in your post