#!/usr/bin/env bash

brt_hash() {
  local algo="${1:-sha256}"
  local input="${2:-}"

  if [[ -z "$input" ]]; then
    brt_error "Usage: brt -hash <md5|sha1|sha256|sha512> <string|file>"
    exit 1
  fi

  algo=$(echo "$algo" | tr '[:upper:]' '[:lower:]')

  local cmd=""
  case "$algo" in
    md5)    cmd="md5" ;;
    sha1)   cmd="shasum -a 1" ;;
    sha256) cmd="shasum -a 256" ;;
    sha512) cmd="shasum -a 512" ;;
    *)
      brt_error "Unsupported algorithm: ${algo}"
      exit 1
      ;;
  esac

  if [[ -f "$input" ]]; then
    $cmd "$input" | awk '{print $1}'
  else
    printf '%s' "$input" | $cmd | awk '{print $1}'
  fi
}
