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
Step 2: To calculate the checksum for this file using the command line, use the below command.
sha256sum GFG.txt
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
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
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
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
Generating an SHA-256 Hash From the Command LineWhat Is SHA256?SHA256SUM of burnt media
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. In this article, we will have a look at how to generate an SHA-256 Hash/Checksum using the command line.
What Is SHA256?
SHA-256 (Secure Hash Algorithm 256-bit) is a cryptographic hash function designed by the NSA and part of the SHA-2 family. It produces a 256-bit (32-byte) fixed-size hash value from an input of any length. SHA-256 is widely used for security applications and protocols, including TLS and SSL, for integrity verification and digital signatures. It is considered secure and resistant to collisions, where two different inputs produce the same hash value. SHA-256 operates by repeatedly applying a series of bitwise operations and modular additions to process data in 512-bit chunks.
Note: Refer to this article to generate a SHA-256 Hash
sha256sum on Linux
sha256sum
is a command-line utility in Linux used to compute and verify SHA-256 hash values of files. You can use it to ensure data integrity and verify that files have not been altered.
To generate a SHA-256 checksum for a file:
sha256sum filename
To verify a file against a provided checksum:
sha256sum -c checksumfile
The checksumfile
should contain the expected hash followed by the filename.