Search code examples
streamchecksumdenomd5sumsha1sum

How can I get a file checksum in Deno?


Just starting with Deno, I am trying to figure out how to calculate a binary file checksum. It seems to me that the problem is not with the methods provided by the hash module of the standard library, but with the file streaming method and/or the type of the chunks feeding the hash.update method. I have been trying a few alternatives, related to file opening and chunk types,with no success. A simple example is in the following:

import {createHash} from "https://deno.land/[email protected]/hash/mod.ts";

const file= new File(["my_big_folder.tar.gz"], "./my_big_folder.tar.gz");
const iterator = file.stream() .getIterator();
const hash = createHash("md5");
for await( let chunk of iterator){
   hash.update(chunk);
}
console.log(hash.toString()); //b35edd0be7acc21cae8490a17c545928

This code compiles and runs with no errors, pity that the result is different from what I get running the functions of the crypto module provided by node and the md5sum provided by linux coreutils. Any suggestion ?

nodejs code:

const crypto = require('crypto');
const fs = require('fs');

const hash = crypto.createHash('md5');

const file = './my_big_folder.tar.gz';
const stream = fs.ReadStream(file);
stream.on('data', data=> { hash.update(data); });
stream.on('end', ()=> {
  console.log(hash.digest('hex')); //c18f5eac67656328f7c4ec5d0ef5b96f
});

The same result in bash:

$ md5sum ./my_big_folder.tar.gz
$ c18f5eac67656328f7c4ec5d0ef5b96f ./my_big_folder.tar.gz

on Windows 10 this can be used:

CertUtil -hashfile ./my_big_folder.tar.gz md5

Solution

  • The File API isn't used to read a File in Deno, to do that you need to use the Deno.open API and then turn it into an iterable like this

    import {createHash} from "https://deno.land/[email protected]/hash/mod.ts";
    
    const hash = createHash("md5");
    
    const file = await Deno.open(new URL(
      "./BigFile.tar.gz",
      import.meta.url, //This is needed cause JavaScript paths are relative to main script not current file
    ));
    for await (const chunk of Deno.iter(file)) {
      hash.update(chunk);
    }
    console.log(hash.toString());
    
    Deno.close(file.rid);