Wednesday 11 September 2019

How to calculate checksum in React Native and Node?

I'm uploading an image file from React Native to AWS Lambda (Node 10.x) and want to verify the hash of the file I've sent matches the file received. To do this I'm using hashing in React Native and again in Lambda, but the hashes never match. Here are the relevant bits of code I've tried.

React Native

import RNFS from "react-native-fs";
const contentChecksum = await RNFS.hash(post.contentUrl, "md5");

Lambda (Node)

import AWS from "aws-sdk";
const crypto = require("crypto");
const s3 = new AWS.S3();

const data = await s3
    .getObject({
      Bucket: file.bucket,
      Key: file.key
    })
    .promise();
const contentChecksum = crypto
    .createHash("md5")
    .update(data.Body)
    .digest("hex");

These checksums never match. I've tried using base64 encoding in Node (data.Body.toString("base64")) and also sha256. What is the trick to calculating the checksum so they match in React Native and Node?

Edit: Here are the results from a recent test.

post.contentUrl: file:///Users/xxxxxxx/Library/Developer/CoreSimulator/Devices/2F2F4FD3-574E-40D7-BE6B-7080E926E70A/data/Containers/Data/Application/65A3FF67-98B2-444D-B75D-3717C1274FBC/Library/Caches/Camera/FDCD8F90-D24F-4E64-851A-96AB388C4B59.jpg

(the file is local on an iPhone)

contentChecksum from React Native: 48aa5cdb30f01719a2b12d481dc22f04

contentChecksum from Node (Lambda): 7b30b61a55d2c39707082293c625fc10

data.Body is a Buffer.

I also note that the eTag attribute on the S3 object matches the md5 checksum I'm calculating in Node. Since eTag is usually the md5 hash of the file, this tells me that I'm likely calculating the hash incorrectly in React Native, but I'm not sure how. I'm using the hash function from the react-native-fs package.



from How to calculate checksum in React Native and Node?

No comments:

Post a Comment