To check the sha1 of a file I would use openssl sha1 <file> I'm not sure what to use to check the sha256 hash of the file however, what would you recommend?

up vote 93 down vote accepted

You can use openssl sha -sha256 <file> or openssl sha256 <file>.

To check command line options for the openssl sha command: openssl sha -help.

  • 11
    openssl sha256 <file> does not work, openssl sha -sha256 <file> did though, thanks! – erikvold Mar 14 '16 at 17:25
  • This is also portable and runs on ubuntu. – Christian Bongiorno Nov 8 '17 at 20:57
  • both commands worked for me on macOS High Sierra. – Yassine El Badaoui Nov 27 '17 at 7:45

OS X ships with a shasum command.

> which shasum
/usr/bin/shasum

You can use:

> shasum -a 256 <file>

More details:

> shasum --help
Usage: shasum [OPTION]... [FILE]...
Print or check SHA checksums.
With no FILE, or when FILE is -, read standard input.

  -a, --algorithm   1 (default), 224, 256, 384, 512, 512224, 512256
  -b, --binary      read in binary mode
  -c, --check       read SHA sums from the FILEs and check them
  -t, --text        read in text mode (default)
  -p, --portable    read in portable mode
                        produces same digest on Windows/Unix/Mac
  -0, --01          read in BITS mode
                        ASCII '0' interpreted as 0-bit,
                        ASCII '1' interpreted as 1-bit,
                        all other characters ignored

The following two options are useful only when verifying checksums:
  -s, --status      don't output anything, status code shows success
  -w, --warn        warn about improperly formatted checksum lines

  -h, --help        display this help and exit
  -v, --version     output version information and exit

When verifying SHA-512/224 or SHA-512/256 checksums, indicate the
algorithm explicitly using the -a option, e.g.

  shasum -a 512224 -c checksumfile

The sums are computed as described in FIPS-180-4.  When checking, the
input should be a former output of this program.  The default mode is to
print a line with checksum, a character indicating type (`*' for binary,
` ' for text, `?' for portable, `^' for BITS), and name for each FILE.

Report shasum bugs to mshelor@cpan.org
  • Hmm I don't seem to have it on osx 10.11.3. which shashum outputs nothing – erikvold Mar 14 '16 at 17:24
  • 1
    @erikvold You know what? I bet I got with the Xcode Command Line tools. Argh. I really wish Apple wouldn't pollute /usr/bin with optional stuff. I'll have to verify this is the case later today. Will update answer if it indeed came from the XCL install. – Ian C. Mar 14 '16 at 19:41
  • shasum returns a different hash than openssl sha -sha256 <file>(with the latter being the correct hash). Any idea why? – ws6079 Jul 21 '17 at 18:02
  • @ws6079 shasum is a perl script, uses Digest::SHA to compute the hash value. For the same file I get the exact same SHA using either shasum or openssl for a SHA-256 hash computation. See: gist.github.com/ianchesal/82a064b8971eb5e717ce84f3ded6dbfd – Ian C. Jul 22 '17 at 7:21

The shasum command ships with OSX for a while now. It will give same result as openssl sha -sha256 when using shasum using the -a 256 option.

I would use this command.

shasum -a 256 -c <<<'paste hash to compare here *path to file goes here'

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.