How to use the command 'shasum' (with examples)
The shasum
command is a versatile tool used for calculating SHA cryptographic checksums. It is useful in a variety of scenarios where verifying the integrity and authenticity of files is important. By generating a unique SHA hash, users can confirm that a file has not been altered or tampered with. The shasum
command supports multiple SHA algorithms, including SHA1, SHA256, and SHA512, among others.
Use Case 1: Calculate the SHA1 checksum for one or more files
Code:
shasum path/to/file1 path/to/file2 ...
Motivation:
Generating a SHA1 checksum for one or more files is crucial when you need to ensure that files have not been altered, particularly when files are transferred between systems. By comparing the checksum generated on both the source and destination, one can verify that the files are identical.
Explanation:
shasum
: This is the command to invoke the checksum calculation process.path/to/file1 path/to/file2 ...
: These are placeholders for the paths to the files for which you want to calculate checksums. You can specify one or more files.
Example Output:
5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8 path/to/file1
7d793037a0760186574b0282f2f435e7c94e97a8 path/to/file2
Use Case 2: Calculate the SHA checksum for one or more files with the specified algorithm
Code:
shasum --algorithm 1|224|256|384|512|512224|512256 path/to/file1 path/to/file2 ...
Motivation:
Different projects might require different levels of security, which is where different SHA algorithms come into play. When a stronger hash is needed, such as SHA256 or SHA512, you can specify this using the --algorithm
option.
Explanation:
shasum
: Initiates the checksum generation.--algorithm
: This option allows the user to specify which SHA algorithm to use, ranging from SHA1 to SHA512256.path/to/file1 path/to/file2 ...
: Indicates the files for which checksums will be calculated.
Example Output:
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 path/to/file1
bf2afee1cc3800ce12e4e1aaeda3f7b58e784b2e5e5c4f5be8350e6b50e00f53 path/to/file2
Use Case 3: Calculate a SHA1 checksum from stdin
Code:
command | shasum
Motivation:
Capturing a checksum from stdin
is practical when you’re dealing with dynamically generated data or streaming data. This could apply when output from a command needs to be validated directly without intermediate files.
Explanation:
command
: Represents any shell command whose output you need to hash.|
: This is the pipe operator, used to pass the output of one command as input to another.shasum
: The command processes the input coming from another command via the pipe.
Example Output:
c3ab8ff13720e8ad9047dd39466b3c89ed7f8d6ae1eb58fa6e37808860ee5467 -
Use Case 4: 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:
Saving a list of checksums to a file is important for long-term integrity checks. In scenarios where multiple files need to be validated over time or across different systems, storing their checksums allows verification against possibly corrupted or tampered files.
Explanation:
shasum
: Begins the checksum calculation.--algorithm 256
: Specifies using the SHA256 algorithm for generating the checksum.path/to/file1 path/to/file2 ...
: Files to be checked.>
: Redirects the output to a file.path/to/file.sha256
: The file where the calculated checksums will be stored.
Example Output:
The output is not directly displayed on the terminal but saved to the specified file (path/to/file.sha256
).
Use Case 5: Read a file of SHA checksums and filenames and verify all files have matching checksums
Code:
shasum --check path/to/file
Motivation:
Verification of files against a previously stored list of checksums is essential for ensuring data integrity. This is particularly useful after data transfers or downloads to confirm files have not been altered.
Explanation:
shasum
: Invokes the checksum verification.--check
: Tellsshasum
to verify the checksums against listed filenames.path/to/file
: The file containing previously calculated checksums and their associated filenames.
Example Output:
path/to/file1: OK
path/to/file2: OK
Use Case 6: Only show a message for missing files or when verification fails
Code:
shasum --check --quiet path/to/file
Motivation:
When dealing with a large set of files, extraneous output can be distracting. Using the quiet option helps focus attention only on problematic files, streamlining troubleshooting.
Explanation:
shasum
: Begins the verification process.--check
: Initiates the check against the checksum file.--quiet
: Suppresses normal output; only alerts if an error occurs.path/to/file
: File with checksums and filenames for checking.
Example Output:
path/to/file2: FAILED
Use Case 7: Only show a message when verification fails, ignoring missing files
Code:
shasum --ignore-missing --check --quiet path/to/file
Motivation:
In configurations where missing files are expected and should not raise alarms, you may want to suppress messages regarding missing files but still catch verification failures.
Explanation:
shasum
: Starts the process to verify checksums.--ignore-missing
: Ignores missing files.--check
: Verifies checksum correctness.--quiet
: Limits output to failures only.path/to/file
: The checksum file with file names to verify.
Example Output:
path/to/file3: FAILED
Use Case 8: Check a known SHA checksum of a file
Code:
echo known_sha_checksum_of_the_file path/to/file | shasum --check
Motivation:
This use case simplifies manual verification when you already know the expected checksum for a file and want to confirm its integrity quickly without needing a pre-existing checksum file.
Explanation:
echo
: Outputs the known checksum and filename, serving as an inline input.known_sha_checksum_of_the_file
: The expected checksum you want to verify.path/to/file
: The file to check against the known checksum.|
: Pipe theecho
output toshasum
.shasum --check
: Initiates verification of the checksum against the input.
Example Output:
path/to/file: OK
Conclusion
Using shasum
offers a robust method for ensuring file integrity and security, whether you’re verifying downloads, checking data transfers, or maintaining data consistency over time. By understanding different use cases and options for generating and verifying checksums, you can apply this versatile tool to various scenarios confidently.