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

后端 未结 6 945
醉酒成梦
醉酒成梦 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

    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
      })
    

提交回复
热议问题