shasum - macOS


Overview

shasum computes and outputs the SHA hash (Secure Hash Algorithm) for a file or string. It is used to verify data integrity and identify duplicate files.

Syntax

shasum [-a algorithm] [-t type] [file1 ...]

Options/Flags

  • -a algorithm: Specifies the hash algorithm to use. Supported algorithms include:
    • sha256 (default)
    • sha512
    • sha384
    • sha224
    • sha1 (deprecated)
  • -t type: Specifies the output format:
    • text (default): Hash value followed by filename
    • binary: Hash value only
  • file1 …: One or more files to hash

Examples

Compute SHA256 hash of a file:

shasum file.txt

Compute SHA512 hash of multiple files:

shasum -a sha512 file1.txt file2.txt

Output hash value only:

shasum -t binary file.txt

Verify data integrity:

shasum -a sha256 file.txt
# The output should match the hash stored in a known good file

Common Issues

  • “command not found” error: Ensure shasum is installed on your system. You can install it using brew install coreutils or port install coreutils.
  • Invalid algorithm specified: Use one of the supported algorithms listed in the -a option.
  • Permission denied: Ensure you have read permissions for the file you want to hash.

Integration

shasum can be combined with other commands for various tasks:

  • Verify a downloaded file:
curl -O https://example.com/file.pkg
shasum -a sha256 -t binary file.pkg | diff - expected.sha256
  • Create a hash database:
find . -type f -print0 | xargs -0 shasum -a sha256 | sort > hashes.txt
  • md5sum: Computes MD5 hash of a file
  • sha256sum: Computes SHA256 hash using a more portable method
  • sha512sum: Computes SHA512 hash using a more portable method