Cookie Consent by Free Privacy Policy Generator 📌 How To Get The Hash of A File In Node.js


✅ How To Get The Hash of A File In Node.js


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

While working on a project, I wanted to do an integrity check of a file that I was referencing. So, I needed to know how to get the hash of a file in Node.js. And this post is about that.

We will use the fs and crypto modules that are available in Node.js to get the hash of a file. We will be using the createReadStream method of the fs module to read the file and get its contents. After we are done reading it, we will call the the getHash() method of the crypto module to calculate the hash of the file.

const fs = require('fs');
const crypto = require('crypto');

const getHash = path => new Promise((resolve, reject) => {
 const hash = crypto.createHash('sha256');
 const rs = fs.createReadStream(path);
 rs.on('error', reject);
 rs.on('data', chunk => hash.update(chunk));
 rs.on('end', () => resolve(hash.digest('hex')));
})

Then, we can use the getHash method to get the hash of a file. It is worth mentioning that we could have used various algorithms for hashing our file, like md5, sha1 and sha256. sha256 would be the more robust algorithm but a bit slower than the other less secure ones. For the digest method, we could have used hex or base64 depending on how we want to output the hash. We can use the above method like so:

(async () => {
  try {
    const hashValue = await getHash('path/to/file');
    console.log(hashValue);
  } catch (error) {
    console.error('Error:', error);
  }
})();

And that is all to the code and its explanation. If you have any questions, feel free to drop a comment below.

...

✅ How To Get The Hash of A File In Node.js


📈 28.98 Punkte

✅ CVE-2024-24553 | Bludit 2.0 Password Hash weak password hash


📈 24.9 Punkte

✅ CVE-2019-20062 | MFScripts YetiShare up to 4.5.4 Hash Expired weak password hash


📈 24.9 Punkte

✅ OnlineVotingSystem up to 1.1.1 Password Hash hash without salt


📈 24.9 Punkte

✅ How to Generate an MD5 File Hash in Node.js?


📈 23.94 Punkte

✅ Using Node File Router for file-based routing in Node.js


📈 22.98 Punkte

✅ Juniper Junos up to 19.1 Configuration File Hash log file


📈 20.13 Punkte

✅ Node.js bis 4.8.3/5.x/6.11.0/7.10.0/8.1.3 Hash Table Flooding Denial of Service


📈 20.1 Punkte

✅ How to Hash Passwords with bcrypt in Node.js


📈 20.1 Punkte

✅ Node.js up to 4.8.3/5.x/6.11.0/7.10.0/8.1.3 Hash Table input validation


📈 20.1 Punkte

✅ cue-sdk-node on Node.js Download ZIP File weak encryption


📈 19.14 Punkte

✅ mkclean 0.8.9 MKV File corec/corec/node/node.c Node_ValidatePtr denial of service


📈 19.14 Punkte

✅ mkvalidator 0.5.1 MKV File corec/corec/node/node.c Node_GetData denial of service


📈 19.14 Punkte

✅ cue-sdk-node auf Node.js Download ZIP File schwache Verschlüsselung


📈 19.14 Punkte

✅ mkvalidator 0.5.1 MKV File corec/corec/node/node.c Node_GetData Denial of Service


📈 19.14 Punkte

✅ mkclean 0.8.9 MKV File corec/corec/node/node.c Node_ValidatePtr Denial of Service


📈 19.14 Punkte

✅ stringhasher - A simple utility to get an int hash in a certain range from a string.


📈 17.49 Punkte

✅ fHash 3.1.1 - File hash calculator.


📈 16.29 Punkte

✅ Hash Method used in shadow file?


📈 16.29 Punkte

✅ Moxa EDS-G512E 5.1 Build 16072215 Password Hash Backup File information disclosure


📈 16.29 Punkte

✅ Jirafeau up to 3.4.0 Search File by Hash Reflected cross site scripting


📈 16.29 Punkte

✅ Python up to 3.3.5 File Hash denial of service


📈 16.29 Punkte

✅ Hash Hunting: Why File Hashes are Still Important


📈 16.29 Punkte

✅ Jirafeau bis 3.4.0 Search File by Hash Reflected Cross Site Scripting


📈 16.29 Punkte

✅ Moxa EDS-G512E 5.1 Build 16072215 Password Hash Backup File Cleartext Information Disclosure


📈 16.29 Punkte











matomo

Datei nicht gefunden!