How to hash files in MacOS terminal

How to hash files in MacOS terminal

Let's say that you have to check the sum of a file you just downloaded. The hash is SHA-256. You would run this command:

shasum -a 256 file_name

Output looks like this:

bla@macos Downloads % shasum -a 256 kali-linux-2023.3-installer-netinst-amd64.iso 
0b0f5560c21bcc1ee2b1fef2d8e21dca99cc6efa938a47108bbba63bec499779  kali-linux-2023.3-installer-netinst-amd64.iso

If you ahve OpenSSL installed then you can also do this:

openssl sha256 file_name

Output looks like this:

bla@macos Downloads % openssl sha256 kali-linux-2023.3-installer-netinst-amd64.iso 
SHA2-256(kali-linux-2023.3-installer-netinst-amd64.iso)= 0b0f5560c21bcc1ee2b1fef2d8e21dca99cc6efa938a47108bbba63bec499779

I personally prefer the openssl command because you have all these algorithms available:

Message Digest commands (see the `dgst' command for more details)
blake2b512        blake2s256        md4               md5               
mdc2              rmd160            sha1              sha224            
sha256            sha3-224          sha3-256          sha3-384          
sha3-512          sha384            sha512            sha512-224        
sha512-256        shake128          shake256          sm3

With shasum command you have these algorithms:

  -a, --algorithm   1 (default), 224, 256, 384, 512, 512224, 512256

All algorithms from shasum command are supported by openssl command but it's interesting to know both methods.