Generate and Verify SHA-1 Hash of File in Linux

Generate and Verify SHA-1 Hash of File in Linux

SHA-1 is a cryptographic hash function that can be used to verify file integrity. For example, to check that the file has not been modified during transfer over the network. This tutorial shows how to generate and verify SHA-1 hash of file in Linux.

Generate SHA-1 hash

For testing purpose, create a new file:

printf 'Hello world' > test.txt

Linux provides sha1sum command for generating and verifying SHA-1 hash. Execute the following command to generate SHA-1 hash of file and print result in the terminal:

sha1sum test.txt

Output:

7b502c3a1f48c8609ae212cdfb639dee39673f5e  test.txt

Use > redirection operator to write result to a file:

sha1sum test.txt > test.sha1

Verify SHA-1 hash

Use -c option to verify file integrity:

sha1sum -c test.sha1

Output:

test.txt: OK

Append additional text to change content of a file:

printf 'Hi' >> test.txt

SHA-1 hash of a file was verified again, we get output:

test.txt: FAILED
sha1sum: WARNING: 1 computed checksum did NOT match

Leave a Comment

Cancel reply

Your email address will not be published.