which-electron/src/fingerprint.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-07-16 06:24:17 +00:00
const DB = require("electron-fingerprints");
2021-07-16 14:46:29 +00:00
const fs = require("fs");
const hasha = require("hasha");
const allVersions = require("./versions")["all"];
2021-07-16 14:46:29 +00:00
function checksumFile(algorithm, path) {
return new Promise(function(resolve, reject) {
let fs = require("fs");
let crypto = require("crypto");
2021-07-16 14:46:29 +00:00
let hash = crypto.createHash(algorithm).setEncoding("hex");
2021-07-16 14:46:29 +00:00
fs.createReadStream(path)
.once("error", reject)
2021-07-16 14:46:29 +00:00
.pipe(hash)
.once("finish", function() {
2021-07-16 14:46:29 +00:00
resolve(hash.read());
});
});
}
2021-07-16 06:24:17 +00:00
module.exports = {
guessFromHashes: function(os, arch, hashList) {
2021-07-16 14:46:29 +00:00
let lookupTable = DB[`${os}-${arch}`];
let allPossibleHashes = Object.keys(lookupTable);
2021-07-16 06:24:17 +00:00
const intersectingHashes = allPossibleHashes.filter((value) =>
hashList.includes(value)
);
// Set it to the starting list of versions.
2021-07-16 14:46:29 +00:00
let possibleVersions = allVersions;
2021-07-16 06:24:17 +00:00
for (i in hashList) {
let hash = hashList[i];
let versions = lookupTable[hash];
2021-07-16 14:46:29 +00:00
if (versions) {
possibleVersions = possibleVersions.filter((value) =>
versions.includes(value)
);
}
2021-07-16 06:24:17 +00:00
}
if (possibleVersions == allVersions) {
return [];
} else {
return possibleVersions;
}
2021-07-16 06:24:17 +00:00
},
2021-07-16 14:46:29 +00:00
getHashes: function(dir) {
let list = fs.readdirSync(dir);
return list.map((f) => {
let fn = `${dir}/${f}`;
return hasha.fromFileSync(fn, { algorithm: "sha1" });
});
2021-07-16 14:46:29 +00:00
},
2021-07-16 06:24:17 +00:00
};