-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (46 loc) · 1.53 KB
/
index.js
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
var fs = require("fs");
const path = require("path");
var crypto = require("crypto");
function isNotBackgroundFile(file) {
if(path.extname(file) === ".css" || path.extname(file) === ".js") {
return true;
}
else {
return false;
};
};
function generate384(absoluteFilepath, algorithm) {
var buffer = fs.readFileSync(absoluteFilepath);
var body = buffer.toString();
const enc = "utf8";
var hash = crypto.createHash(algorithm).update(body, enc);
var sha = hash.digest("base64");
return algorithm + "-" + sha;
};
function saveSubresourceIntegrityHash(staticFilepath, algorithm, json) {
if (algorithm == "sha256" || algorithm == "sha384" || algorithm == "sha512") {
const filesInDirectory = fs.readdirSync(staticFilepath);
for (const file of filesInDirectory) {
const absoluteFilepath = path.join(staticFilepath, file);
if (fs.statSync(absoluteFilepath).isDirectory()) {
saveSubresourceIntegrityHash(absoluteFilepath, algorithm, json);
}
else if (isNotBackgroundFile(file)) {
var newHash = {
"filepath": absoluteFilepath,
"hash": generate384(absoluteFilepath, algorithm)
};
json.push(newHash);
};
};
}
else {
console.log("Incorrect algorithm, please select a value supported by browsers for sub-resource integrity checking ('sha256', 'sha384', 'sha512').");
};
};
function get(staticFilepath, algorithm) {
var json = [];
saveSubresourceIntegrityHash(staticFilepath, algorithm, json);
return json;
};
module.exports = {get};