Motivation

Be able to check the sha256 sums on the files downloaded from internet

Installation

  1. Copy the script file into a Scripts folder in your Home directory (at the same level as Documents Downloads etc.)
  2. Make the file executable sudo chmod a+x hash-check_sha256
  3. Add the Scripts into path in your ~/.bashrc file by adding a following line at the end
PATH="$HOME/Scripts:$PATH"
  1. Add the alias hash-check into the aliases into the ~/.bashrc or ~/.bash_aliases file using following command
echo "alias hash-check=hash-check_sha256" >> ~/.bash_aliases

How to use it

  1. Downolad the file which hash sum you want to check
  2. Copy the sha256sum into clipboard
  3. Type hash-check <file-name> <sha256sum from clipboard>

Script

File to download

hash-check_sha256

Code

#!/bin/bash

[ "$#" -ne 2 ] && echo "Usage: $0 <filename> <expected_hash>" && exit 1

FILE="$1"
EXPECTED="$2"

[ ! -f "$FILE" ] && echo "Error: File not found!" &&	exit 1

CALCULATED=$(sha256sum "$FILE" | awk '{print $1}')

echo "Expected:   $EXPECTED"
echo "Calculated: $CALCULATED"

[ "$CALCULATED" = "$EXPECTED" ] && echo "Hash matches!"  || echo "Hash does NOT match!"