const fs = require("fs");
const crypto = require("crypto-js");
function hash(content) {
try {
// First hash (WordArray hash)
const wordArray = crypto.lib.WordArray.create(content);
const firstHash = crypto.SHA256(wordArray);
const firstHashHex = firstHash.toString(crypto.enc.Hex);
console.log("First hash:" + firstHashHex);
// Second hash (WordArray hash)
const firstHashWordArray = crypto.enc.Hex.parse(firstHashHex);
const secondHash = crypto.SHA256(firstHashWordArray);
const secondHashHex = secondHash.toString(crypto.enc.Hex);
console.log("Double hash:" + secondHashHex);
// Returns the double hash
return secondHashHex;
} catch (err) {
console.log(`Error hashing the file content: ${err}`);
throw err;
}
}
const filePath = "./filetohash.pdf";
const fileContent = fs.readFileSync(filePath, {});
hash(fileContent);