function md5File(filename, cb) {
if (typeof cb !== 'function') throw new TypeError('Argument cb must be a function')
var output = crypto.createHash('md5')
var input = fs.createReadStream(filename)
input.on('error', function (err) {
cb(err)
})
output.once('readable', function () {
cb(null, output.read().toString('hex'))
})
input.pipe(output)
}n/a
function md5FileAsPromised(filename) {
return new Promise(function (resolve, reject) {
md5File(filename, function (err, hash) {
if (err) return reject(err)
resolve(hash)
})
})
}n/a
function md5FileSync(filename) {
var fd = fs.openSync(filename, 'r')
var hash = crypto.createHash('md5')
var buffer = new Buffer(BUFFER_SIZE)
try {
var bytesRead
do {
bytesRead = fs.readSync(fd, buffer, 0, BUFFER_SIZE)
hash.update(buffer.slice(0, bytesRead))
} while (bytesRead === BUFFER_SIZE)
} finally {
fs.closeSync(fd)
}
return hash.digest('hex')
}...
md5File('LICENSE.md', (err, hash) => {
if (err) throw err
console.log(`The MD5 sum of LICENSE.md is: ${hash}`)
})
/* Sync usage */
const hash = md5File.sync('LICENSE.md')
console.log(`The MD5 sum of LICENSE.md is: ${hash}`)
```
## Promise support
If you require `md5-file/promise` you'll receive an alternative API where all
functions that takes callbacks are replaced by `Promise`-returning functions.
...function md5FileAsPromised(filename) {
return new Promise(function (resolve, reject) {
md5File(filename, function (err, hash) {
if (err) return reject(err)
resolve(hash)
})
})
}n/a
function md5FileSync(filename) {
var fd = fs.openSync(filename, 'r')
var hash = crypto.createHash('md5')
var buffer = new Buffer(BUFFER_SIZE)
try {
var bytesRead
do {
bytesRead = fs.readSync(fd, buffer, 0, BUFFER_SIZE)
hash.update(buffer.slice(0, bytesRead))
} while (bytesRead === BUFFER_SIZE)
} finally {
fs.closeSync(fd)
}
return hash.digest('hex')
}...
md5File('LICENSE.md', (err, hash) => {
if (err) throw err
console.log(`The MD5 sum of LICENSE.md is: ${hash}`)
})
/* Sync usage */
const hash = md5File.sync('LICENSE.md')
console.log(`The MD5 sum of LICENSE.md is: ${hash}`)
```
## Promise support
If you require `md5-file/promise` you'll receive an alternative API where all
functions that takes callbacks are replaced by `Promise`-returning functions.
...