How To Get The Hash of A File In Node.js (2024)

How To Get The Hash of A File In Node.js (1)

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.

Top comments (0)

Subscribe

For further actions, you may consider blocking this person and/or reporting abuse

How To Get The Hash of A File In Node.js (2024)
Top Articles
Life after debt: Venture debt funding could grow again in 2024
Deciphering Mergers and Acquisitions (M&A) vs. Private Equity: Are They the Same?
Time in Baltimore, Maryland, United States now
Directions To Franklin Mills Mall
Craigslist Cars Augusta Ga
News - Rachel Stevens at RachelStevens.com
Google Jobs Denver
Craigslist Furniture Bedroom Set
Victoria Secret Comenity Easy Pay
Tripadvisor Near Me
What Was D-Day Weegy
2021 Lexus IS for sale - Richardson, TX - craigslist
4302024447
8 Ways to Make a Friend Feel Special on Valentine's Day
Accuradio Unblocked
سریال رویای شیرین جوانی قسمت 338
Teenleaks Discord
Justified Official Series Trailer
Used Sawmill For Sale - Craigslist Near Tennessee
Alexander Funeral Home Gallatin Obituaries
Who called you from +19192464227 (9192464227): 5 reviews
Craigs List Tallahassee
Low Tide In Twilight Ch 52
Afni Collections
Helpers Needed At Once Bug Fables
Progressbook Newark
Used Safari Condo Alto R1723 For Sale
Duke Energy Anderson Operations Center
3473372961
J&R Cycle Villa Park
6465319333
Moonrise Time Tonight Near Me
Orangetheory Northville Michigan
Pensacola Cars Craigslist
Rage Of Harrogath Bugged
Barber Gym Quantico Hours
Bones And All Showtimes Near Johnstown Movieplex
Lovely Nails Prices (2024) – Salon Rates
Noaa Duluth Mn
Obituaries in Hagerstown, MD | The Herald-Mail
Despacito Justin Bieber Lyrics
Powerspec G512
Atu Bookstore Ozark
Toomics - Die unendliche Welt der Comics online
Walmart 24 Hrs Pharmacy
Yale College Confidential 2027
Joblink Maine
Brutus Bites Back Answer Key
Tanger Outlets Sevierville Directory Map
Vrca File Converter
Land of Samurai: One Piece’s Wano Kuni Arc Explained
One Facing Life Maybe Crossword
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5896

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.