Created
March 3, 2018 16:50
-
-
Save FOLLGAD/2ca1e6612877a7a403ea00f92adead9d to your computer and use it in GitHub Desktop.
Node.js thesubdb.com hash implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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