UNPKG

820 BJavaScriptView Raw
1var fs = require('fs')
2var crypto = require('crypto')
3var unyield = require('unyield')
4var thunkify = require('thunkify')
5var join = require('path').join
6
7var readFile = thunkify(fs.readFile)
8
9/**
10 * Gets the hash of a single file.
11 *
12 * hashFile('image.jpg')
13 */
14
15exports.hashFile = unyield(function * (fname, digest) {
16 var shasum = crypto.createHash(digest || 'sha1')
17
18 try {
19 var data = yield readFile(fname)
20 } catch (e) {
21 return null
22 }
23
24 shasum.update(data)
25 return shasum.digest('hex')
26})
27
28/**
29 * Hashes multiple files.
30 */
31
32exports.hashFiles = unyield(function * (root, paths) {
33 var hashes = yield paths.map(function (path) {
34 return exports.hashFile(join(root, path))
35 })
36
37 var result = {}
38
39 paths.forEach(function (path, idx) {
40 result[path] = hashes[idx]
41 })
42
43 return result
44})