1

I am trying to get the checksum or the hash of a file from a server without download the whole file to my server or writing it on my disk like reading the data from the server and then getting the hash value.

Code

var download = function (url, dest, callback) {

                request.get(url)
                    .on('error', function (err) { console.log(err) })
                    .pipe(fs.createWriteStream(dest))
                    .on('close', callback);

            };

            final_list.forEach(function (str) {
                var filename = str.split('/').pop();

                console.log('Downloading ' + filename);

                download(str, filename, function () { console.log('Finished Downloading' + "" + filename) });
            });

hash

   function generateChecksum(str, algorithm, encoding) {
        return crypto
            .createHash(algorithm || 'md5')
            .update(str, 'utf8')
            .digest(encoding || 'hex');
    }
Jhon Caylog
  • 483
  • 8
  • 24
  • https://stackoverflow.com/questions/14733374/how-to-generate-an-md5-file-hash-in-javascript/33486055#33486055 – Kaus2b Oct 11 '19 at 02:11
  • @KausUntwale: That’s for browser JavaScript. – Ry- Oct 11 '19 at 02:11
  • Similar question asked by the OP here: https://stackoverflow.com/questions/58319517/is-it-possible-to-get-the-md5-hash-value-of-a-file-being-downloaded-from-a-serve – jfriend00 Oct 11 '19 at 02:27
  • its way far , we are asking for answers or ideas not about similar ones, – Jhon Caylog Oct 11 '19 at 02:28

1 Answers1

1

Hash objects from Node’s crypto module are writable streams. You can pipe directly to them, something like:

request.get(url)
    .pipe(crypto.createHash('md5'))
    .on('readable', function () {
        callback(null, this.read());
    });

(Error handling left as an exercise to the reader, because error handling with streams in Node is the absolute worst.)

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • How do i log the hashed values ? – Jhon Caylog Oct 11 '19 at 02:17
  • But this reads the entire stream which the OP said they didn't want to do. – jfriend00 Oct 11 '19 at 02:19
  • @JhonCaylog: `this.read()` once the Hash has emitted `'readable'` is the hash digest, as a buffer. Try logging `this.read().toString('hex')`, for example. – Ry- Oct 11 '19 at 02:39
  • @jfriend00: I think “download the whole file to my server” means the same thing as “writing it to my disk” in the question. It’s just impossible otherwise. – Ry- Oct 11 '19 at 02:39
  • now how are we going to access the data outside so i can use it to query data in mongo ang compare – Jhon Caylog Oct 14 '19 at 05:11