Ad

How To Use Nodejs Crypto In CryptoMd5Method Of EvaporateJS

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.

Ad

Answer

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(_), ...

Ad
source: stackoverflow.com
Ad