forked from todogroup/repolinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-hashes-not-exist.js
More file actions
72 lines (63 loc) · 2.05 KB
/
file-hashes-not-exist.js
File metadata and controls
72 lines (63 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const Result = require('../lib/result')
const crypto = require('crypto')
// eslint-disable-next-line no-unused-vars
const FileSystem = require('../lib/file_system')
/**
* Check files' hashes not included in a list of certain cryptographic hashes.
*
* @param {FileSystem} fs A filesystem object configured with filter paths and target directories
* @param {object} options The rule configuration
* @returns {Promise<Result>} The lint rule result
* @ignore
*/
async function fileHashesNotExist(fs, options) {
const fileList = options.globsAll || options.files
const files = await fs.findAllFiles(fileList, !!options.nocase)
if (files.length === 0) {
return new Result(
'Did not find any file matching the specified patterns',
fileList.map(f => {
return { passed: false, pattern: f }
}),
true
)
}
const algorithm = options.algorithm || 'sha256'
const resultsList = await Promise.all(
options.hashes.map(async hash => {
const singleHashResults = (
await Promise.all(
files.map(async file => {
const digester = crypto.createHash(algorithm)
let fileContents = await fs.getFileContents(file)
if (fileContents === undefined) {
fileContents = ''
}
digester.update(fileContents)
const fileHash = digester.digest('hex')
const passed = fileHash !== hash
const message = passed ? "Doesn't Matches hash" : 'Match hash'
return {
passed,
path: file,
message
}
})
)
).filter(result => !result.passed)
return singleHashResults
})
)
const results = []
resultsList.map(singleHashResults => {
for (const result of singleHashResults) {
results.push(result)
}
})
const passed = results.length === 0
if (passed) {
return new Result('No file matching hash found', results, passed)
}
return new Result('File matching has found', results, passed)
}
module.exports = fileHashesNotExist