As a software developer, I often find myself knee-deep in the world of crypto. Now, if you’re on the same boat as me, or even just curious, you’ve probably come across hashes, right? Well, in today’s post, I’m going to walk you through how to create a SHA3-512 hash in Node.js. Whether you’re a professional or just getting started in the world of programming, this guide is designed to give you a clear understanding of generating SHA3-512 hashes and effectively using them in your Node.js projects. So, let’s dive right into it, shall we?
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 512 bits, or SHA 512. Although there are numerous variations, SHA 512 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-512 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 or 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-512 can act as a stamp or for checking if the data is valid or not.
The 512 in the name SHA3-512 refers to the final hash digest value, meaning that regardless of the amount of plaintext or cleartext, the hash value will always be 512 bits.
For example –
Input String | Output Hash |
---|---|
hi | 154013cb8140c753f0ac358da6110fe237481b26c75c3ddc1b59eaf9dd7b46a0a3aeb2cef164b3c82d65b38a4e26ea9930b7b2cb3c01da4ba331c95e62ccb9c3 |
debugpointer | ac09d8e98cd7d60927600334167ca22a79fad316a8af4fdc1d076c6cfe72c44778cc47223eb296f3ddc13dcfd4bb395f6346c0f29f2f2f4a46f4d9bcee63d1df |
computer science is amazing! I love it. | 308a9fd755db73031ddae5f58734c8b57db3bc5ea24bfc6de7df962158e24bbe697219a72ccdeaa03c1848f2c35ef46d050ec0b678465de309e8a43bdf248c64 |
If you want to generate SHA3-512 checksum in JavaScript i.e., client side (browser), please follow this article – Create SHA3-512 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-512 hash in nodejs script/code, we shall use the default crypto
module that comes packaged with nodejs.
Creating SHA3-512 Hash of a String
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";
import { createHash } from "crypto";
const yourString = "This works";
const hash = createHash("sha3-512").update(yourString).digest("hex");
console.log(hash);
The output of the above script will be an SHA3-512 hash when you run the command node index.js
in your shell-
d847a8a72496900d82e8f6ef89587f4cef974c6220a38ac2dc715753d36a3e8b0020603f4bb0c8c2072383c3cb83735e06481a0a618b0bfb733f72a5c8a81ff5
Creating SHA3-512 Hash of a Password
In case you are looking at creating an SHA3-512 hash of a password, which is the most common use-case of hashing, you can use the createHash
function and update the password variable and create a hex digest of it.
Here is an example of creating SHA3-512 hash of a password variable.
import { createHash } from "crypto";
const password = "Hello@123";
const passhash = createHash("sha3-512").update(password).digest("hex");
console.log(passhash);
The output of the above script will be an SHA3-512 hash when you run the command node index.js
in your shell-
dcda1dde9fb10da7fd41ef1565bd4080eb4abe848759842c8133bae138b8b9922ac6dc2c6dc118b0410d795999f486f1642a0c3215768fbc03f8115770bdd82d
Passwords can also be SHA3-512 hashed in the frontend JavaScript, but, its not advised to do it in the frontend, as your hash is now known to the attacker, eventually leading to a security breach.
The above code just produced SHA3-512 hash of the string alone, but, to strengthen the security you can also generate SHA3-512 hash with salt as well.
If you looking to create an SHA3-512 hash of a file, please follow the article where we discuss as to how to read a file buffer and create the hash.
Prefer SHA3-256 or SHA3-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-512 hash in Node.js.
I’m glad that you found the content useful. And there you have it! We’ve just journeyed together through the process of creating a SHA3-512 hash in Node.js. Looking back, I hope you now see that it’s not as complex as it first seemed. I’m confident that with this knowledge, you’re now well-equipped to handle any project that requires SHA3-512 hashes. But remember, practice is key! The more you work with Node.js and SHA3-512, the more comfortable you’ll become. And as always, should you encounter any bumps along the way, don’t hesitate to refer back to this post. Happy Coding.