Obtaining the hash of a file using the stream capabilities of crypto module (ie: without hash.update and hash.digest)

后端 未结 6 923
醉酒成梦
醉酒成梦 2020-12-04 15:25

The crypto module of node.js (at the time of this writing at least) is not still deemed stable and so the API may change. In fact, the methods that everyone in the internet

相关标签:
6条回答
  • 2020-12-04 16:00

    An ES6 version returning a Promise for the hash digest:

    function checksumFile(hashName, path) {
      return new Promise((resolve, reject) => {
        const hash = crypto.createHash(hashName);
        const stream = fs.createReadStream(path);
        stream.on('error', err => reject(err));
        stream.on('data', chunk => hash.update(chunk));
        stream.on('end', () => resolve(hash.digest('hex')));
      });
    }
    
    0 讨论(0)
  • 2020-12-04 16:11
    var fs = require('fs');
    var crypto = require('crypto');
    var fd = fs.createReadStream('data.txt');
    var hash = crypto.createHash('md5');
    hash.setEncoding('hex');
    fd.pipe(hash);
    hash.on('data', function (data) {
        console.log('# ',data);
    });
    
    0 讨论(0)
  • 2020-12-04 16:21

    Short version of Carlos' answer:

    var fs = require('fs')
    var crypto = require('crypto')
    
    fs.createReadStream('/some/file/name.txt').
      pipe(crypto.createHash('sha1').setEncoding('hex')).
      on('finish', function () {
        console.log(this.read()) //the hash
      })
    
    0 讨论(0)
  • 2020-12-04 16:21

    Further polish, ECMAScript 2015

    function checksumFile(algorithm, path) {
      return new Promise((resolve, reject) =>
        fs.createReadStream(path)
          .on('error', reject)
          .pipe(crypto.createHash(algorithm)
            .setEncoding('hex'))
          .once('finish', function () {
            resolve(this.read())
          })
      )
    }
    
    0 讨论(0)
  • 2020-12-04 16:21

    I use Node module hasha successfully, the code becomes very clean and short. It returns a promise, so you can use it with await:

    const hasha = require('hasha');
    
    const fileHash = await hasha.fromFile(yourFilePath, {algorithm: 'md5'});
    
    0 讨论(0)
  • 2020-12-04 16:22

    From the quoted snippet in the question:

    [the Hash class] It is a stream that is both readable and writable. The written data is used to compute the hash. Once the writable side of the stream is ended, use the read() method to get the computed hash digest.

    So what you need to hash some text is:

    var crypto = require('crypto');
    
    // change to 'md5' if you want an MD5 hash
    var hash = crypto.createHash('sha1');
    
    // change to 'binary' if you want a binary hash.
    hash.setEncoding('hex');
    
    // the text that you want to hash
    hash.write('hello world');
    
    // very important! You cannot read from the stream until you have called end()
    hash.end();
    
    // and now you get the resulting hash
    var sha1sum = hash.read();
    

    If you want to get the hash of a file, the best way is create a ReadStream from the file and pipe it into the hash:

    var fs = require('fs');
    var crypto = require('crypto');
    
    // the file you want to get the hash    
    var fd = fs.createReadStream('/some/file/name.txt');
    var hash = crypto.createHash('sha1');
    hash.setEncoding('hex');
    
    fd.on('end', function() {
        hash.end();
        console.log(hash.read()); // the desired sha1sum
    });
    
    // read all file and pipe it (write it) to the hash object
    fd.pipe(hash);
    
    0 讨论(0)
提交回复
热议问题