How to use the command shasum (with examples)

How to use the command shasum (with examples)

The shasum command is used to calculate SHA cryptographic checksums. It is typically used to verify the integrity of files by comparing their checksums before and after transmission or storage.

Use case 1: Calculate the SHA1 checksum for one or more files

Code:

shasum path/to/file1 path/to/file2 ...

Motivation: This use case is useful when you want to calculate the SHA1 checksum of one or more files to ensure their integrity.

Explanation: The shasum command is followed by the paths of the files for which you want to calculate the SHA1 checksum. The command will then calculate and display the SHA1 checksum for each file.

Example output:

8749c8b6f4495e48d340a41425607c83e9769716  path/to/file1
2032d189f209f25e6a97c502add14f4637f495fe  path/to/file2

Use case 2: Calculate the SHA256 checksum for one or more files

Code:

shasum --algorithm 256 path/to/file1 path/to/file2 ...

Motivation: This use case is useful when you want to calculate the more secure SHA256 checksum for one or more files.

Explanation: The --algorithm flag is used to specify the desired algorithm, in this case, 256 for SHA256. The shasum command is followed by the paths of the files for which you want to calculate the SHA256 checksum. The command will then calculate and display the SHA256 checksum for each file.

Example output:

bb5a7f5104563877aa38a34ba168b60de277725a2f72e86c59242b0b67bd5ef3  path/to/file1
c03e2ea4399c7433e66e7aeef50e240e6c96e04347ce85f161d249f363c4785e  path/to/file2

Use case 3: Calculate the SHA512 checksum for one or more files

Code:

shasum --algorithm 512 path/to/file1 path/to/file2 ...

Motivation: This use case is useful when you want to calculate the most secure SHA512 checksum for one or more files.

Explanation: The --algorithm flag is used to specify the desired algorithm, in this case, 512 for SHA512. The shasum command is followed by the paths of the files for which you want to calculate the SHA512 checksum. The command will then calculate and display the SHA512 checksum for each file.

Example output:

c3748a98e014e7f6e58f92bca57411bff745630060eb791a5b37968d9d0404f01559659efc9624d5f61d16a7f5b8c15e7308dce05e0ab11e0063b0e0fbf389d  path/to/file1
a264b50d5ed84e454c97eab6d6da446bdb6e20bce42a04e7f3e4e3271a46b6d31d3034a957e59a1a8c391b2e85cc7a8ad186dc7da7127f7cf1de43d4b68dfb4d  path/to/file2

Use case 4: Calculate a SHA1 checksum from stdin

Code:

command | shasum

Motivation: This use case is useful when you want to calculate the SHA1 checksum of data provided through Standard Input (stdin).

Explanation: The shasum command is used as a pipeline command, following the vertical bar (|) symbol. Data is piped into the shasum command through stdin, and then the command will calculate and display the SHA1 checksum.

Example output:

16c7ab099f217bad3fad1efb6f07f5ff27286d46  -

Use case 5: Calculate and save the list of SHA256 checksums to a file

Code:

shasum --algorithm 256 path/to/file1 path/to/file2 ... > path/to/file.sha256

Motivation: This use case is useful when you want to calculate the SHA256 checksum for one or more files and save the checksums to a file for future reference.

Explanation: The --algorithm flag is used to specify the desired algorithm, in this case, 256 for SHA256. The shasum command is followed by the paths of the files for which you want to calculate the SHA256 checksum. The command is then redirected using the > symbol to save the output, which contains the list of SHA256 checksums, to a specified file.

Example output: The file file.sha256 will contain the following:

bb5a7f5104563877aa38a34ba168b60de277725a2f72e86c59242b0b67bd5ef3  path/to/file1
c03e2ea4399c7433e66e7aeef50e240e6c96e04347ce85f161d249f363c4785e  path/to/file2

Use case 6: Read a file of SHA1 sums and filenames and verify all files have matching checksums

Code:

shasum --check path/to/file

Motivation: This use case is useful when you have a file containing SHA1 sums and filenames and you want to verify that the current files have matching checksums.

Explanation: The --check flag is used to perform verification. The shasum command is followed by the path to the file containing SHA1 sums and filenames. The command will then read the file and verify that the current files have matching SHA1 checksums.

Example output:

path/to/file1: OK
path/to/file2: OK

Use case 7: Only show a message for missing files or when verification fails

Code:

shasum --check --quiet path/to/file

Motivation: This use case is useful when you want to perform verification but only display a message when there are missing files or when verification fails.

Explanation: The --check flag is used to perform verification. The --quiet flag is used to suppress output, except for missing files or failed verification. The shasum command is followed by the path to the file containing SHA1 sums and filenames. The command will then read the file and perform verification, displaying a message only for missing files or when verification fails.

Example output:

path/to/file1: OK
path/to/file2: FAILED

Use case 8: Only show a message when verification fails, ignoring missing files

Code:

shasum --ignore-missing --check --quiet path/to/file

Motivation: This use case is useful when you want to perform verification, but only display a message when there are checksum mismatches, ignoring any missing files.

Explanation: The --ignore-missing flag is used to ignore missing files during verification. The --check flag is used to perform verification. The --quiet flag is used to suppress output, except for failed verification. The shasum command is followed by the path to the file containing SHA1 sums and filenames. The command will then read the file and perform verification, displaying a message only for checksum mismatches, but ignoring any missing files.

Example output:

path/to/file2: FAILED

Conclusion:

The shasum command is a versatile tool for calculating and verifying SHA cryptographic checksums. It offers various options for different use cases, allowing users to check the integrity of files and ensure their data has not been tampered with. Whether you need to calculate checksums, perform verification, or save checksums to a file, the shasum command has you covered.

Related Posts

How to use the command 'aiac' (with examples)

How to use the command 'aiac' (with examples)

The ‘aiac’ command is a tool that leverages OpenAI to generate Infrastructure as Code (IaC) configurations, utilities, queries, and more.

Read More
How to use the command 'path' (with examples)

How to use the command 'path' (with examples)

The ‘path’ command in Windows is used to display or set the search path for executable files.

Read More
How to use the command `pkg_delete` (with examples)

How to use the command `pkg_delete` (with examples)

The pkg_delete command is used in OpenBSD to remove packages. It is typically used with the pkg_add and pkg_info commands to manage packages in the OpenBSD operating system.

Read More