Generate and Verify SHA-512 Hash of File in Linux

Generate and Verify SHA-512 Hash of File in Linux

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

Generate SHA-512 hash

Create a new file for testing:

printf 'Hello world' > test.txt

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

sha512sum test.txt

Output:

b7f783baed8297f0db917462184ff4f08e69c2d5e5f79a942600f9725f58ce1f29c18139bf80b06c0fff2bdd34738452ecf40c488c22a7e3d80cdf6f9c1c0d47  test.txt

Result can be written to a file by using > redirection operator:

sha512sum test.txt > test.sha512

Verify SHA-512 hash

Use -c option to verify file integrity:

sha512sum -c test.sha512

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
sha512sum: WARNING: 1 computed checksum did NOT match

Leave a Comment

Cancel reply

Your email address will not be published.