Skip to content

Instantly share code, notes, and snippets.

@smartwatermelon
Created July 21, 2022 05:09
Show Gist options
  • Save smartwatermelon/d6f7a7e9a45c05f9b95ce7a3a23eeded to your computer and use it in GitHub Desktop.
Save smartwatermelon/d6f7a7e9a45c05f9b95ce7a3a23eeded to your computer and use it in GitHub Desktop.
@cassidoo's 20220717 interview question
#!/usr/bin/env bash
set -eu -o pipefail
# substitute asterisks for all but first and last characters of string
obscureString () {
str="$1"
ret="${str:0:1}$( printf "%0.s*" $( seq 1 $(( ${#str}-2 )) ) )${str: -1}"
echo $ret
}
# check for one param split with '@'
if [ $# -eq 0 ] || [ $# -gt 2 ] || [[ ! "$1" =~ ^.*@.*[^@]$ ]]; then
echo "$(basename "$0"): enter a valid email address"
exit 1
fi
local_part=$( cut -d '@' -f 1 <<< "$1" )
domain=$( cut -d '@' -f 2 <<< "$1" )
# let's make sure the domain is legit
if dig $domain | grep "ANSWER: 0" 1>/dev/null; then
echo "$domain is not valid"
exit 1
fi
# substitute asterisks for middle portion of local_part
local_part_subbed=$( obscureString "$local_part" )
if [ $# -gt 1 ] && [[ "${2,,}" == "hidefull" ]]; then
# substitute asterisks for middle portion of domain
domain_subbed=$( obscureString "$domain" )
echo "${local_part_subbed}@${domain_subbed}"
else
echo "${local_part_subbed}@${domain}"
fi
@smartwatermelon
Copy link
Author

MONTASIO:~ andrewrich$ ~/Documents/scripts/hideEmail.sh [email protected]
a*********[email protected]
MONTASIO:~ andrewrich$ ~/Documents/scripts/hideEmail.sh [email protected] hidefull
a*********h@g*******m
MONTASIO:~ andrewrich$ ~/Documents/scripts/hideEmail.sh [email protected]
fakemail.fake is not valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment