You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
808 B

// Native
const { createHash } = require('crypto')
// Packages
const { readFile } = require('fs-extra')
9 years ago
/**
* Computes hashes for the contents of each file given.
*
* @param {Array} of {String} full paths
* @return {Map}
*/
9 years ago
async function hashes(files) {
const map = new Map()
await Promise.all(
files.map(async name => {
const data = await readFile(name)
const h = hash(data)
const entry = map.get(h)
if (entry) {
entry.names.push(name)
} else {
map.set(hash(data), { names: [name], data })
}
})
)
return map
9 years ago
}
/**
* Computes a hash for the given buf.
*
* @param {Buffer} file data
* @return {String} hex digest
*/
function hash(buf) {
return createHash('sha1').update(buf).digest('hex')
9 years ago
}
module.exports = hashes