Published on

Create SHA-512 Hash in Node.js

Create SHA-512 Hash in Node.js

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.

The SHA-512 hashing algorithm is currently one of the best and secured hashing algorithms after hashes like MD5 and SHA-1 has been broken down. Due to their complicated nature it is not well accepted and SHA-256 is a general standard, but the industry is slowing moving towards this hashing algorithm.

SHA 256 is a part of the SHA 2 family of algorithms, where SHA stands for Secure Hash Algorithm. It was a joint effort between the NSA and NIST to introduce a successor to the weaker SHA 1 family. SHA2 was published in 2001 and has been effective ever since.

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. SHA-512 can act as a stamp or for checking if the data is valid or not.

SHA-512 hasn't gained the popularity that SHA-256 is experiencing or even other types of newer hashing algorithms when it comes to real-world use in blockchain. That being said it does have a few non-blockchain applications that are noteworthy.

The 512 in the name SHA-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 StringOutput Hash
computer science is amazing! I love it.d46bc2b8b0e30ee6f2bfe42826a01d550451223e36d8ea73e46f283eeed3514480b16681ebb9ad8d72c7f9247b5711e5f0797578200afe8229abf86b6ade79cd

If you want to generate SHA-512 checksum in JavaScript i.e., client side (browser), please follow this article - Create SHA-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 SHA-512 hash in nodejs script/code, we shall use the default crypto module that comes packaged with nodejs.

Creating SHA-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("sha512").update(yourString).digest("hex")

The output of the above script will be an SHA-512 hash when you run the command node index.js in your shell-


Creating SHA-512 Hash of a Password

In case you are looking at creating an SHA-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 SHA-512 hash of a password variable.

import { createHash } from "crypto"
const password = "Hello@123"
const passhash = createHash("sha512").update(password).digest("hex")

The output of the above script will be an SHA-512 hash when you run the command node index.js in your shell-


Passwords can also be [SHA-512 hashed in the frontend JavaScript](/javascript/create-sha512-hash-in-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 SHA512 hash of the string alone, but, to strengthen the security you can also generate [SHA512 hash with salt](/nodejs/create-sha512-hash-with-salt-in-nodejs) as well.

If you looking [to create an SHA-512 hash of a file](/nodejs/create-sha512-hash-of-file-in-nodejs), please follow the article where we discuss as to how to read a file buffer and create the hash.

Prefer [SHA-256](/nodejs/create-sha256-hash-in-nodejs) 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 SHA-512 hash in Node.js.

I'm glad that you found the content useful. Happy Coding.