Generating a SHA-256 hash from the Linux command line


There are plenty of ways to generate a hash on any operating system, but when we talk about generating an almost-unique and fixed size bit hash, then nothing can replace the SHA algorithm.

Before making use of the Linux command to generate a SHA-256 hash, we must know what SHA actually is and what it is good for.

SHA-256 in very simple terms is a cryptographic hash function that has a digest length of 256 bits. It is an algorithm on its own that is able to generate an almostunique and fixed size 256-bit(32-byte) hash. It is also good to know that this algorithm was actually developed by the NSA (National Security Agency).

Now that we know a little bit about what SHA256 means, let’s explore a simple case where we will print a random hash function that is generated using the SHA256 algorithm against a string value.

Example

Consider the example shown below −

I like Tutorialspoint

If we use the SHA256 algorithm on the above string and convert it into a hash we will get the following output −

1b2ca228e3847e330f006772aa0af2cd307f0ae92c6722cbb0c1533a84ba5339

This might look very interesting at first, and it surely is.

Now, let’s explore two examples where we will learn how we can generate a SHA256 hash on the linux command line.

In the first case, we will make use of the command shown below −

echo -n "TutorialPoint" | openssl dgst -sha256

Output

(stdin)= 62e2de2644fa0987f79f54118c175d6a924e50aa60df1ff38e197eac0da8a963

In the second example, we can remove the openssl and the dgst command and just write the sha256sum command and we will have our hash function generated like above.

Consider the example shown below −

Command

echo -n “TutorialsPoint” | sha256sum

Output

62e2de2644fa0987f79f54118c175d6a924e50aa60df1ff38e197eac0da8a963

We can use either of these approaches as they more or less have the same effect on your operating system.

Updated on: 29-Jul-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements