This script takes a string of characters of any length and mangles it, randomly transforming upper-to-lower case, spaces to underscores or minus signs, some letters to digits and so on. For each character in the string, the script uses nawk to generate a random 50/50 chance (like flipping a coin) thus determining whether to mangle the character or leave it unchanged.
This script has recently been updated to allow it to function standalone (using the options and arguments) or to receive input piped from the pwgen script.
#!/bin/sh
#
#set -vx
verbose=FALSE
while getopts p:v r
do
case $r in
p) pwstring=$OPTARG;;
v) verbose=TRUE;;
esac
done
if [ -z "$pwstring" ]; then
echo "Password to mangle: "
read pwstring
fi
nospace=`echo $pwstring | sed -e 's/ /_/g'`
if [ $verbose = "TRUE" ]; then
echo "converted spaces to underscores:"
echo $nospace
echo "Your mangled password is:"
fi
# use nawk because that allows random numbers
echo $nospace | nawk '{
srand()
a = $1 ; l = length(a)
j = "= + ! _ - @ . :"
k = split(j,s," ")
for (i=1; i <= l; i++)
{
# Isolate each character of the input one at a time
b=substr(a,i,1)
# Change the case of the input character (50/50 chance)
n=int(rand()*2)
if (n == 0)
{
c=tolower(b)
}
if (n == 1)
{
c=toupper(b)
}
# Change a to @ symbol (50/50 chance)
if ( b == "a" )
{
n=int(rand()*2)
if (n == 0)
{
c="@"
}
}
# Change B to digit 8 (50/50 chance)
if ( b == "B" )
{
n=int(rand()*2)
if (n == 0)
{
c="8"
}
}
# Change g to digit 9 (50/50 chance)
if ( b == "g" )
{
n=int(rand()*2)
if (n == 0)
{
c="9"
}
}
# Change E to digit 3 (50/50 chance)
if ( b == "E" )
{
n=int(rand()*2)
if (n == 0)
{
c="3"
}
}
# Change i or I to ! (50/50 chance)
if ( b == "i" || b == "I")
{
n=int(rand()*2)
if (n == 0)
{
c="!"
}
}
# Change s or S to 5 (50/50 chance)
if ( b == "s" || b == "S")
{
n=int(rand()*2)
if (n == 0)
{
c="5"
}
}
# Change o or O to 0 (zero , 50/50 chance)
if ( b == "o" || b == "O")
{
n=int(rand()*2)
if (n == 0)
{
c="0"
}
}
# Change underscore to minus sign (50/50 chance)
if (b == "_")
{
n=int(rand()*2)
if (n == 0)
{
c = "-"
}
}
# Change t to plus sign (50/50 chance)
if (b == "t")
{
n=int(rand()*2)
if (n == 0)
{
c = "+"
}
}
# Change minus sign to random joiner (50/50 chance)
if (b == "-")
{
n=int(rand()*2)
if (n == 0)
{
x=int(rand()*8)+1
c=s[x]
}
}
# Print out the results
#printf "%s flip %s to %s \n" , b,n,c
printf "%s", c
}
print ""
}'