7

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.

Fook
  • 4,220
  • 5
  • 25
  • 45
  • What do the outputs look like and what is the value of `post.contentUrl` and `data.Body`? – tbking Aug 27 '19 at 18:56
  • @tbking I've added more info in the question. – Fook Aug 27 '19 at 19:36
  • Looks like an encoding issue. Try `data.Body.toString()` in node. – tbking Aug 27 '19 at 19:44
  • That didn't seem to help. – Fook Aug 28 '19 at 19:29
  • access the file object in react-native and take the hash of it, and similar on lambda, use the file content and take hash of the content again, that should be the logic. – Mohit Yadav Sep 10 '19 at 18:37
  • Isn't that what I'm doing in the code? `RNFS.hash()` calculates the hash of the file in react-native, and `crypto.createHash("md5").update(data.Body).digest("hex")` does the same in lambda. Can you provide an example of what you mean? – Fook Sep 10 '19 at 18:42
  • Can you generate th MD5 using an external program to see if at least one of the 2 output is right. In Win10: CertUtil -hashfile image.jpg MD5 – Eric Villemure Sep 17 '19 at 11:34

2 Answers2

1

You can use the same code on React and AWS Lambda, that is Node.js.

So in your React.js application you could use the following code:

import * as React from 'react';
import crypto from 'crypto';

var key = 'YOUR_KEY';

export default class Test extends React.Component {

    render() {
        var hash = crypto.createHash('md5').update(key).digest('hex');
        return (
            <div>
                {hash}
            </div>
        )
    }

}

And the variable hash have to contains the same value you get on AWS.

In order to run you have to install the crypto library:

npm i --save react-native-crypto

Change the variable YOUR_KEY, then run the application:

npm start

And in the browser you should get:

4b751fef5e9660e3943173fd3e6c4224
Alessandro
  • 4,032
  • 4
  • 26
  • 59
1

You can use the crypto module.

To get a list of all available hash algorithms, you can use crypto.getHashes().

Here is a Nodejs example:

var crypto = require('crypto')

crypto.getHashes() // [ 'dsa', 'dsa-sha', ..., 'md5', ... ]

Here is a helper method for generating checksum value from string input:

var crypto = require('crypto')

function checksum(str, algorithm, encoding) {
  return crypto
    .createHash(algorithm || 'md5')
    .update(str, 'utf8')
    .digest(encoding || 'hex')
}

checksum('This is my test text'); 
checksum('This is my test text', 'sha1');
Samy Bencherif
  • 1,101
  • 10
  • 23
Savaj Patel
  • 357
  • 1
  • 10