'expo-crypto hash not matching with the hash generated by Crypto JS in react web app
I am trying to hash the content(bytes without the meta information) of an image file in my React native mobile app and React web app. I am using expo-crypto to hash the image in the mobile version and CryptoJS in the web app version. But the hash generated by both these methods are not same.
Can someone please help me figure out where there is a hash mismatch.
RN Expo code
'''
let info = await FileSystem.readAsStringAsync(path);
var digest ;
if(info)
{ digest = await Crypto.digestStringAsync(
Crypto.CryptoDigestAlgorithm.SHA256,
info
);) }
'''
React Code
'''
function handleUpload(event) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = (event) => {
resolve(event.target.result);
var hash = sha256(event.target.result)
};
reader.onerror = (err) => {
reject(err);
};
reader.readAsBinaryString(event.target.files[0]);
}); }
'''
Solution 1:[1]
What are you using for Crypto in react-native? expo-crypto doesn't support binary. You will have to use something like isomorphic-webcrypto to hash binary. The compile options: https://docs.expo.dev/versions/latest/sdk/crypto/ is how the hash(digest) is returned, no way to modify input processing even though this would be a really easy change.
Check this out for more background and a potential workaround: https://forums.expo.dev/t/how-to-hash-buffer-with-expo-for-an-array/45337
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
