Published on

Create SHA-512 Hash in Javascript

Create SHA-512 Hash in Javascript

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-512 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

The methods below are for creating it at client-side or browser. If you are looking to generate SHA-512 checksum in nodejs, please follow this article - Create SHA-512 Hash in Node.js and if you are looking to create SHA-512 Hash of a file you can check this article.

Method 1 - Using crypto-js SHA-512 hash in HTML code

Here we will be using the above npm package directly in HTML code. We are using version 4.1.1 of the crypto-js package. Let's use the Cloudflare CDN links and use <script> tags to import core.min.js and sha512.js scripts.

After that, you can use it in your code as CryptoJS.SHA-512(yourString). Here is an example demonstrating crypto-js and SHA-512 implementation-

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src=""></script>
    <script src=""></script>
      var hash = CryptoJS.SHA512("This works")

The output of the above code as an alert and in the logs will be-


Method 2 : Using crypto-js SHA-512 hash using ES6 and require

Let's get into the modern approach first, using ES6 and crypto-js module in the frontend. Here, can import the function SHA-512 from the package. You can then use it directly to create an SHA-512 hash as shown in the example below-

First install the npm package-

$ bower install crypto-js

Let's configure it in require-

    paths: {
        'crypto-js': 'path-to/bower_components/crypto-js/crypto-js'

Then you can use it in your code-

require(["crypto-js"], function (CryptoJS) {
    console.log(CryptoJS.SHA512("Hello All"));

The output of the above code in the console will be-


It's your choice, use what works best for you.

Prefer SHA-256 or SHA-512 or other superior cryptographic hash functions for creating a hash for passwords, integrity verification.

Related articles

Let me summarize creating a hash in JavaScript and Nodejs, so that it becomes easy for you to refer-

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