Navigating the digital world, I’ve often found myself grappling with the need for enhanced security measures. Hashing has proven to be a potent tool in my coding arsenal. And today, I want to let you in on how to create a SHA3-256 hash of a file in Node.js. If you’re wondering what that even means, fret not! I’ll walk you through the concept of hashing, the specifics of SHA3-256, and how Node.js comes into play. So, ready to jump in?
Encryption and hashing have served as the foundation for new security modules, among other network security developments. One of the most used hash algorithms is the Secure Hash Algorithm(SHA) with a digest size of 256 bits, or SHA 256. Although there are numerous variations, SHA 256 has been the most often used in practical applications. There are weaker predecessors to SHA3 like MD5, SHA1, SHA2. Interested in knowing the difference between SHA1, SHA2 & SHA3, this will give you a great insight on how SHA has evolved over the years.
Introduction to SHA3-256 Hashing
SHA-3 (Secure Hash Algorithm 3) is the latest member of the Secure Hash Algorithm family of standards. Although part of the same series of standards, SHA-3 is internally different from the MD5-like structure of SHA-1 and SHA-2. SHA-3 instances are drop-in replacements for SHA-2, intended to have identical security properties. The SHA-3 family consists of six hash functions with digests (hash values) that are 128, 224, 256, 384 or 512 bits: SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256.
The SHA-3 / Keccak algorithm is one of the most secure and efficient hashing algorithms and some claim that it won’t be cracked in the next 20 – 30 years. Developments in the quantum computing world might decrease that time frame but it is still one of the best hashing algorithm we have got right now.
The hash function generates the same output hash for the same input string. This means that, you can use this string to validate files or text or anything when you pass it across the network or even otherwise. SHA3-256 can act as a stamp or for checking if the data is valid or not.
The 256 in the name SHA3-256 refers to the final hash digest value, meaning that regardless of the amount of plaintext or cleartext, the hash value will always be 256 bits.
For example –
Input String | Output Hash |
---|---|
hi | b39c14c8da3b23811f6415b7e0b33526d7e07a46f2cf0484179435767e4a8804 |
debugpointer | 5bd28b8b5e1c0c8355362e581d0c478842ee79840adfe0307139179a2ff5d5de |
computer science is amazing! I love it. | 9d9c88852fed897f23a898f0994b325cff9c70b629c96eff3739c4ffb1459edf |
If you want to generate SHA3-256 checksum in JavaScript i.e., client side (browser), please follow this article – Create SHA3-256 Hash in JavaScript
Node.js crypto
module provides cryptographic functions to help you secure code and data in Node.js. It includes a set of wrappers for OpenSSL’s hash, HMAC, cipher, decipher, sign, and verify functions.
crypto
is built into Node.js, so there is not configuration or custom implementation needed.
For creating SHA3-256 hash in nodejs script/code, we shall use the default crypto
module that comes packaged with nodejs.
If you looking to create an MD5 hash of a string, please follow the article where we discuss about this.
Creating SHA3-256 Hash of a File
Now let’s learn how to create an SHA3-256 hash of a file. File can be any file, a text file, a JSON file, data file etc.,
You can either require
the crypto
module-
const crypto = require("crypto");
or also use the modern import
to import the crypto
module-
import { createHash } from "crypto";
Here, let’s consider a text file for example – hello.txt
having content – Welcome to Debugpointer.
Let’s read the contents of the file using the fs
module and read the file in a synchronous manner.
import { createHash } from "crypto";
const fs = require("fs");
const buff = fs.readFileSync("hello.txt");
const hash = createHash("sha3-256").update(buff).digest("hex");
console.log(hash);
The output of the above script will be an SHA3-256 hash when you run the command node index.js
in your shell-
5a9bca39c0583dcdabca4943cce8ffcbff51e699c73f8210a4a88ea6c9191a51
Prefer SHA3-256 or SHA-512 or other superior cryptographic hash functions for creating a hash for passwords, integrity verification.
It’s as simple as that! You do not need any fancy npm library for creating an SHA3-256 hash in Node.js.
Well, that’s a wrap, folks! I hope this guide provided some insight into how to create a SHA3-256 hash of a file in Node.js. It’s a handy skill to have in your programming toolkit, especially in a world where data integrity and security are paramount. And remember, this is just the tip of the iceberg; the beauty of Node.js is its versatility, allowing you to explore a multitude of different applications and features. So don’t stop here, keep experimenting, keep learning, keep coding, Happy Coding.