0

I'm using EvaporateJS in a react project which uses webpack.

As described in the documentation I used the following:

(I don't want to use the aws-sdk because of the package size which btw works properly)

cryptoMd5Method: function (data) { 
 return crypto.createHash('md5').update(data).digest('base64'); 
}

But 'data' is type of ArrayBuffer. So I tried converting it to string.

cryptoMd5Method: function (data) { 
    var enc = new TextDecoder();
    var dataString = enc.decode(data);
    var computed = crypto.createHash('md5').update(dataString).digest('base64');
    return computed ;
}

But that doesn't compute the digest correctly.


So, what should be the solution for this (considering the nodejs crypto option)?

Also alternatively, how to import just AWS.util.crypto module without referencing the whole aws-sdk ? This will help me keep the bundle small.

Foez Ahmed
  • 93
  • 10

2 Answers2

1

1) Import browser compatible packages:

import MD5 from 'js-md5'; import { sha256 as SHA256 } from 'js-sha256';

2) Declare functions:

const md5 = (x) => { const o = MD5.create(); o.update(x); return o.base64(); }; const sha256 = (x) => { const o = SHA256.create(); o.update(x); return o.hex(); };

3) Usage in config:

... computeContentMd5: true, cryptoMd5Method: (_) => md5(_), cryptoHexEncodedHash256: (_) => sha256(_), ...

Alexander Shagin
  • 485
  • 5
  • 13
  • 1
    It worked! Even though I was hoping to get a solution around 'Crypto' but these 2 libraries are way smaller than the 'aws' sdk size. – Foez Ahmed Apr 26 '20 at 19:20
0

The entire aws-sdk is indeed big, however for front-end (browsers) you can build a smaller artifact that includes only what you need. Follow the instructions over at the amazon docs - Building the SDK for Browsers.

For converting ArrayBuffer to string to pass it to the update() method of the hashing algorithm, see: Converting between strings and ArrayBuffers.

Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48