Open In App

Generating an SHA-256 Hash From the Command Line

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The SHA-256 algorithm is used to check the integrity of the data. The hashes generated using SHA-256 can verify the integrity and authenticity of the data. We can use an SHA-256 checksum/hash, a string of numbers and letters, to determine whether a given file is the same as the original. An extremely different checksum or hash can be produced from a small change in the data. This can be used to determine whether the file was compromised during transmission or storage, either by attackers or by other technical issues. When you have a file that must be 100% accurate, it is a very good idea to perform an SHA-256 hash comparison check. As its name suggests, an SHA-256 hash is 256 bits long.

Note: Refer to this article to generate a SHA-256 Hash

In this article, we will have a look at how to generate an SHA-256 Hash/Checksum using the command line. 

Generate SHA-256 Hashes for a String

In this section, we will take a look at how we can generate the SHA-256 hash for a given string value.

Step 1: We will use the sha256sum command in Linux to do this. Using the below command, you can find out more about the sha256sum command in Linux.

man sha256sum
Manual page of sha256sum command

 

Step 2: To calculate the checksum for a string, using the command line, use the below command.

echo -n Geeks For Geeks | sha256sum 
Calculate the Checksum

 

echo normally outputs a newline, hence, we use the “-n” flag to suppress that.

The generated output looks like this: 

  • The generated checksum [First 65 characters]
  • A simple dash character.

If there is only one dash, it simply means that the file name is absent and the input came from the standard input stream. In order to suppress that from the output, refer to the next step.

Step 3: The SHA-256 checksum for any string can be generated and printed using the aforementioned command. If you only want to print the checksum value and not the extra dash character, then use the below command.

echo -n Geeks For Geeks | sha256sum | awk ‘{ print $1 }’

Print the checksum value and not the extra dash character

 

Step 4: Refer to the image below to see how a simple change in the case of a string’s letters can drastically alter the checksum that is generated in the above command.

echo -n GEEKS FOR GEEKS | sha256sum | awk ‘{ print $1 }’

Changing the String

 

Generate SHA-256 Hashes for a File

In this section, we will take a look at how we can generate the SHA-256 hash for a given file.

Step 1: Firstly, let’s create a text file and fill it with some basic text to show how the command operates. Use the below command to do this.

echo Hello, I am a proud GeekForGeeks user. > GFG.txt

Creating New file GFG.txt

 

Step 2: To calculate the checksum for this file using the command line, use the below command.

sha256sum GFG.txt
Calculate the Checksum

 

The generated output looks like this: 

  • The generated checksum [First 65 characters]
  • The name or path of the file.

Step 3: To store this hash value in a file, use the below command.

sha256sum GFG.txt> checksum
cat checksum
Store the Hash Value in file

 

This will store the checksum/hash value of the given file in another file, as shown in the output image.

Verify the SHA-256 Hash of the File

In this section, we will take a look at how we can verify the generated hashes and use them to verify the integrity of the given file.

Step 1: To verify the authenticity of the file, we will use the hash value stored inside the checksum file in the previous steps. Use the below command to do that.

sha256sum --check checksum 
Check CheckSum

 

Step 2: The output in the above image authenticates the given file and makes sure that its contents are not harmed in any way. Use the below command to alter the contents of the file.

echo I am a proud GeekForGeeks student. > GFG.txt

Alter the contents of the file

 

Step 3: As we can see in the below image, the –check command failed after we updated the contents of the file by just one word. This is how the SHA-256 algorithm is used to verify the authenticity and integrity of the file.

cat checksum
sha256sum --check checksum
Check CheckSum

 

Generate and Verify SHA-256 Hashes of Multiple Files

In this section, we will take a look at how we can generate the SHA-256 hash and verify that for multiple files.

Repeat the steps from the above section, “Generate SHA-256 Hashes For A File” to create one more file and generate the hash for this file. Thereafter, store this hash in the same file. Refer to the below output image for this.

echo This is a article from GeeksforGeeks. > Article.txt
cat Article.txt
sha256sum Article.txt >> checksum 
cat checksum
Store the Hash Value in file

 

If we now perform integrity tests on each entry in the checksum file, it processes each entry and informs us of which files pass and which fail the test. Refer to the below output image for this.

sha256sum --check checksum
Check CheckSum

Output For Checksum Verification Of Multiple Files


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads