Skip to content

Instantly share code, notes, and snippets.

@FOLLGAD
Created March 3, 2018 16:50
Show Gist options
  • Select an option

  • Save FOLLGAD/2ca1e6612877a7a403ea00f92adead9d to your computer and use it in GitHub Desktop.

Select an option

Save FOLLGAD/2ca1e6612877a7a403ea00f92adead9d to your computer and use it in GitHub Desktop.
Node.js thesubdb.com hash implementation
const fs = require('fs'),
crypto = require('crypto')
function getHash(pathToFile) {
let readSize = 64 * 1024 // 64kb
return new Promise((resolve, reject) => {
let fd = fs.openSync(pathToFile, 'r')
let buffer = new Buffer(readSize * 2)
let r1 = fs.read(fd, buffer, 0, readSize, 0)
let r2 = fs.stat(pathToFile).then(s => {
return fs.read(fd, buffer, readSize, readSize, s.size - readSize)
})
Promise.all([r1, r2])
.then(() => {
let hash = crypto.createHash('md5').update(buffer).digest('hex')
resolve(hash)
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment