How to use the command rhash (with examples)
The rhash
command is a utility that allows users to calculate or check common message digests. It supports various hash functions, including CRC32, MD5, SHA1, SHA256, and SHA3.
Use case 1: Calculate default CRC32 digests of a file
Code:
rhash path/to/file
Motivation: Calculating CRC32 digests can help verify the integrity of files during transmission or storage. By comparing the calculated digest with the expected one, you can ensure that the file has not been tampered with.
Explanation:
rhash
: The command itself.path/to/file
: The path to the file for which you want to calculate the CRC32 digest.
Example output:
0528bd87
Use case 2: Recursively process a directory to generate an SFV file using SHA1
Code:
rhash --sha1 --recursive path/to/folder > path/to/output.sfv
Motivation: Generating an SFV (Simple File Verification) file can be useful to verify the integrity of multiple files within a directory structure. The SFV file contains checksums for all the files in the directory, making it easy to verify their integrity at a later time.
Explanation:
--sha1
: Specifies the hash function to use, in this case, SHA1.--recursive
: Enables recursive processing of the directory to include all files within subdirectories.path/to/folder
: The path to the folder that you want to process.> path/to/output.sfv
: Redirects the output (SFV file) to the specified path.
Example output:
; Generated by rhash 1.3.9
;
; SHA1 checksums
;
9e4672a7a44f88ca3a5ca7db1e920d6c4a7165b5 ./path/to/folder/file1.txt
b02db46a81c1a5c005314c542117fba46b734065 ./path/to/folder/file2.txt
Use case 3: Verify the integrity of files based on an SFV file
Code:
rhash --check path/to/file.sfv
Motivation: By verifying the integrity of files using an SFV file, you can ensure that the files have not been modified or corrupted since the SFV file was generated. It can be particularly useful when transferring files or performing backups.
Explanation:
--check
: Instructsrhash
to check the integrity using the SFV file.path/to/file.sfv
: The path to the SFV file that contains the checksums for the files you want to verify.
Example output:
./path/to/folder/file1.txt: OK
./path/to/folder/file2.txt: FAILED
WARNING: 1 computed checksums did NOT match
Use case 4: Calculate the SHA3 digest of a text message
Code:
rhash --sha3-256 --message 'message'
Motivation: Calculating the SHA3 digest of a text message can provide a unique representation of the message. This can be helpful when detecting any changes in the message, such as tampering or corruption.
Explanation:
--sha3-256
: Specifies the SHA3-256 hash function.--message 'message'
: The message for which you want to calculate the SHA3 digest.
Example output:
acd3ffb6b9a9300decf6d593c77148add76670915d9b2e1ee7b6c6f1e8cf1a9b
Use case 5: Calculate CRC32 digest of a file and output digest encoded in base64 using BSD format
Code:
rhash --base64 --bsd path/to/file
Motivation: Encoding the CRC32 digest in base64 can be beneficial in situations where the output needs to be compatible with systems or protocols that expect data in that format, rather than hexadecimal.
Explanation:
--base64
: Instructsrhash
to encode the digest in base64 format.--bsd
: Formats the output similar to BSD utilities, including the separator between the digest and the file path.path/to/file
: The path to the file for which you want to calculate the CRC32 digest.
Example output:
0528bd87 path/to/file
Use case 6: Use custom output template
Code:
rhash --printf '%p\t%s\t%{mtime}\t%m\n' path/to/file
Motivation: Using a custom output template allows you to specify the desired format to display information about the file. This can be useful when you require specific details or need the output to match a specific format for further processing.
Explanation:
--printf '%p\t%s\t%{mtime}\t%m\n'
: Specifies the custom output format. In this example,%p
represents the file path,%s
represents the file size,%{mtime}
represents the modification time, and%m
represents the file mode.path/to/file
: The path to the file for which you want to display the information.
Example output:
path/to/file 12345 1632931254 -rw-r--r--
Conclusion:
The rhash
command provides a comprehensive set of features for calculating and verifying message digests. It supports various hash functions and offers options for customizing the output. Whether you need to calculate checksums, generate verification files, or verify file integrity, rhash
can be a valuable tool in your workflow.