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

后端 未结 6 940
醉酒成梦
醉酒成梦 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: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())
          })
      )
    }
    

提交回复
热议问题