#!/bin/bash

# read in the input string from command line arguments
input=$1

# pad the input string with a single "1" bit
padded_input="$input"$(echo -ne '\x80')

# pad the input string to a multiple of 512 bits (64 bytes)
while (( $(echo -n "$padded_input" | wc -c) % 64 != 56 ))
do
  padded_input="$padded_input"$(echo -ne '\x00')
done

# append the length of the input string (in bits) as a 64-bit little-endian integer
length=$(echo -n "$input" | wc -c)
length=$(echo "obase=16; $length * 8" | bc | xxd -p -c 16 | tac | tr -d '\n')
while (( $(echo -n "$length" | wc -c) < 16 ))
do
  length="$length"0
done
padded_input="$padded_input"$(echo -ne "$length" | xxd -r -p)

# initialize the buffer (A, B, C, D)
A="67452301"
B="efcdab89"
C="98badcfe"
D="10325476"

# process the input in 512-bit (64-byte) chunks
for (( i=0; i<$(echo -n "$padded_input" | wc -c)/64; i++ ))
do
  chunk=$(echo -n "$padded_input" | dd bs=64 skip=$i count=1 2>/dev/null | xxd -p -c 64)

  # initialize the message schedule (M)
  M=()
  for (( j=0; j<16; j++ ))
  do
    word=$(echo -ne "${chunk:$j*8:8}" | xxd -r -p | od -An -tu4 -v)
    M+=($word)
  done
  for (( j=16; j<64; j++ ))
  do
    word1=${M[j-15]}
    s0=$(( (word1>>7 | word1<<25) ^ (word1>>18 | word1<<14) ^ (word1>>3) ))

    word2=${M[j-2]}
    s1=$(( (word2>>17 | word2<<15) ^ (word2>>19 | word2<<13) ^ (word2>>10) ))

    M+=($((M[j-16] + s0 + M[j-7] + s1)))
  done

  # initialize the working variables
  AA=$A
  BB=$B
  CC=$C
  DD=$D

  # round 1
  for (( j=0; j<16; j++ ))
  do
    F=$(( (B & C) | (~B & D) ))
    g=$j
    dTemp=$((D))
    D=$((C))
    C=$((B))
    B=$((B + ((A + F + M[g] + 0xd76aa478) & 0xffffffff)))
    A=$((dTemp))
  done

  # round 2
  for (( j=16; j<32; j++ ))
  do
    F=$(( (D & B) | (~D & C) ))
    g=$(( (5*j + 1) % 16 ))
    dTemp=$((D))
    D=$((C))
    C=$((B))
   
