Skip to content

Instantly share code, notes, and snippets.

@F1LT3R
Created August 1, 2017 04:09
Show Gist options
  • Save F1LT3R/2e4347a6609c3d0105afce68cd101561 to your computer and use it in GitHub Desktop.
Save F1LT3R/2e4347a6609c3d0105afce68cd101561 to your computer and use it in GitHub Desktop.
sha1 hash node.js
const crypto = require('crypto')
const sha1 = path => new Promise((resolve, reject) => {
const hash = crypto.createHash('sha1')
const rs = fs.createReadStream(path)
rs.on('error', reject)
rs.on('data', chunk => hash.update(chunk))
rs.on('end', () => resolve(hash.digest('hex')))
})
sha1('./test.txt').then(hash => {
console.log(hash)
}).catch(err => {
throw new Error(err)
})
@benjaminhoffman
Copy link

dont forget const fs = require('fs') .... it may be implied, but still.

@abdulhannanali
Copy link

@benjaminhoffman is right. 👍

@craigphicks
Copy link

async function hashOfStream(readable) {
    return await once(
        readable.pipe(crypto.createHash('sha1').setEncoding('hex')),
        'finish')
}

@hungdev
Copy link

hungdev commented Sep 5, 2019

thanks bro

@colesiegel
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment