#!/usr/bin/env bash

# Check which cryptographic and non-cryptographic checksum algorithm tools are
# available on a computer.
#
# Copyright (c) 2022 by Reto Kromer <https://reto.ch>
#
# This Bash script is released under a 3-Clause BSD License and is provided
# "as is" without warranty or support of any kind.


# verify if the passed command is installed
verify_command() {
  [[ $(command -v "${1}") ]] &>/dev/null && echo "  ${1}"
}


# print information about the running operating system
echo
printf '%64s\n' | tr " " "-"
echo "      machine hardware: $(uname -m)"
echo "processor architecture: $(uname -p)"
echo "      operating system: $(uname -sr)"
if [[ "$(uname -s)" == "Darwin" ]]; then
  printf '%24s%s\n' ' ' "$(sw_vers -productName) $(sw_vers -productVersion) (build $(sw_vers -buildVersion))"
elif [[ "$(uname -s)" == "Linux" ]]; then
  printf '%24s%s\n' ' ' "$(grep -E '^(VERSION|NAME)=' /etc/os-release)"
fi
printf '%64s\n' | tr " " "-"


# check cryptographic algorithms

# MD5
echo "Installed MD5 checksum tool(s):"
verify_command 'md5'
verify_command 'md5sum'
verify_command 'md5deep'

# SHA
echo "Installed generic SHA checksum tool(s):"
verify_command 'sha'
verify_command 'shasum'
verify_command 'shadeep'

# SHA-1
echo "Installed SHA-1 checksum tool(s):"
verify_command 'sha1'
verify_command 'sha1sum'
verify_command 'sha1deep'

# SHA-256
echo "Installed SHA-256 checksum tool(s):"
verify_command 'sha256'
verify_command 'sha256sum'
verify_command 'sha256deep'

# SHA-512
echo "Installed SHA-512 checksum tool(s):"
verify_command 'sha512'
verify_command 'sha512sum'


# check non-cryptographic algorithms

# CRC-32
echo "Installed CRC-32 checksum tool(s):"
verify_command 'crc32'
verify_command 'crc32sum'
verify_command 'cksum'

# Historic AT&T System V UNIX systems
echo "Installed historic CRC-16 checksum tool:"
verify_command 'sum'

# xxHash 128, xxHash 64 and xxHash 32
echo "Installed xxHash checksum tool:"
verify_command 'xxhsum'


# print coda
printf '%64s\n' | tr " " "-"
echo
