DebugPointer
Published on

Create SHA-256 Hash in Javascript

Create SHA-256 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 256 bits, or SHA 256. Although there are numerous variations, SHA 256 has been the most often used in practical applications.

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

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

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

Method 1 - Using crypto-js SHA-256 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 sha256.js scripts.

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

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>SHA-256</title>
  </head>
  <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/core.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/sha256.js"></script>
    <script>
      var hash = CryptoJS.SHA256("This works")
      alert(hash)
      console.log(hash)
    </script>
  </body>
</html>

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

e74358db452b245573586b48e96ab3504c019e79fbf15e8572c74370f37579c5

Method 2 : Using crypto-js SHA-256 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 SHA256 from the package. You can then use it directly to create an SHA-256 hash as shown in the example below-

First install the npm package-

$ bower install crypto-js

Let's configure it in require-

require.config({
    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.SHA256("Hello All"));
});

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

94733212265f28ec2d0213188aeeaaf3876a996cb94f13f49968bc414ca47f1f

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.