javascriptnode.jshashmd5md5sum

NodeJS: crypto-js ouputs a different md5 hash than bash


So I have the following JS code:

const Crypto = require("crypto-js");
const jsonFile = require('path/to/file.json');


console.log(Crypto.MD5(JSON.stringify(jsonFile)).toString());

The issue is this console.log produces a different hash than when I md5 the file in bash:

md5sum /path/to/file.json

I've tried passing different encodings inside the toString, hashing the javascript object itself, etc but so far I couldn't get the hashes to match.


Solution

  • I suspect that importing, then reformatting your content does not help. I could obtain the same hash by simply reading the file as a string:

    const fs = require('fs');
    const Crypto = require("crypto-js");
    
    const data = fs.readFileSync('./input', 'utf8')
    console.log(Crypto.MD5(data).toString())