#! /bin/sh
# FakeMAC server-side example in Lisp.
# Copyright (C) 2021 Jean Zee (a.k.a. CmpZ)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Documentation is at http://cybertiggyr.com/fakemac.html
#

if test ! -x `which openssl`
then
    echo Error: openssl is not in your path.
    exit 1
fi

# The string to hash
S=$1

# Oh so ugly.  openssl, why do you insist on printing other gunk
# before the actual hash so that we must remove it?
# We could do all this in a single pipelined expression, but code
# like that tends to be read-only.  So we break it into separate steps.
#
# Get the hash from openssl.  It'll be prefixed with something
# like "(STDIN)= ".  The hash code will be in hex, & the letters
# in it will be downcase.
TMP=`echo -n $S |openssl dgst -sha1 -hex`
# Strip that leading "(STDIN)= ".  Technically, we're stripping
# everything before the first "= ", also stripping that "= ".
TMP=`echo $TMP |sed -e "s/^.*= \(.*\)$/\1/"`
# Convert to upcase & return
echo $TMP |tr "[:lower:]" "[:upper:]"
