I am trying to shrink bash script so it do not have to rerun the same codes over and over again. but according to some people i can run the line at test dealing with all 4 encryption at 1 go.
help i do not know what do they meant by that.
Code Gore:
#!/bin/bash
LIST1=(K F L W )
LIST2=(8 2 9 2 )
LIST3=(x b s v )
LIST4=("~" "-" "[" "*" )
LIST5=("$" "+" "]" "%" )
encr1=".8742cNKlzqQ8Mgjip/Fg1"
salt1="bCgxj8Yt"
encr2="31HvJ8Iinxk2k"
salt2="31"
encr3="AyPVCzU.ourSwFdL3N6/YP9RRfIwwKZPNrnt0/yn5vB"
salt3="klPjs90j"
encr4="Cd9AjUI4nGglIcP3MByrZUnu.hHBJc7.eR0o/v0A1gu0/6ztFfBxeJgKTzpgoCLptJS2NnliZLZjO40LUseED/"
salt4="8899Uidd"
for i in "${LIST1[@]}"
do
for j in "${LIST2[@]}"
do
for k in "${LIST3[@]}"
do
for l in "${LIST4[@]}"
do
for a in "${LIST5[@]}"
do
test="$(mkpasswd -m MD5 "$i$j$k$l$a" -s $salt1 | cut -d"$" -f4)"
if [ "$test" == "$encr1" ] ; then
echo " MD5 Salted Hash Password is: $i$j$k$l$a"
echo " Salt: "$salt1""
for i in "${LIST1[@]}"
do
for j in "${LIST2[@]}"
do
for k in "${LIST3[@]}"
do
for l in "${LIST4[@]}"
do
for a in "${LIST5[@]}"
do
test="$(mkpasswd -m SHA-256 "$i$j$k$l$a" -s $salt3 | cut -d"$" -f4)"
if [ "$test" == "$encr3" ] ; then
echo " SHA-256 Salted Hash Password is: $i$j$k$l$a"
echo " Salt: "$salt3""
fi
done
done
done
done
done
for i in "${LIST1[@]}"
do
for j in "${LIST2[@]}"
do
for k in "${LIST3[@]}"
do
for l in "${LIST4[@]}"
do
for a in "${LIST5[@]}"
do
test="$(mkpasswd -m SHA-512 "$i$j$k$l$a" -s $salt4 | cut -d"$" -f4)"
if [ "$test" == "$encr4" ] ; then
echo " SHA-512 Salted Hash Password is: $i$j$k$l$a"
echo " Salt: "$salt4""
for i in "${LIST1[@]}"
do
for j in "${LIST2[@]}"
do
for k in "${LIST3[@]}"
do
for l in "${LIST4[@]}"
do
for a in "${LIST5[@]}"
do
test="$(mkpasswd -m des "$i$j$k$l$a" -s $salt2)"
if [ "$test" == "$encr2" ] ; then
echo " DES Salted Hash Password is: $i$j$k$l$a"
echo " Salt: "$salt2""
exit
fi
done
done
done
done
done
fi
done
done
done
done
done
fi
done
done
done
done
done
The Output still somehow displayed as i wanted:

Since you use the arrays LIST1 ... LIST5 only for the 5×for constructs I would replace them with brace expansions. That way you end up with only one loop.
Instead of manually listing and checking variable pairs like encr1 and salt1 use three arrays encr, salt, and algo (the algorithm you specified manually before). Then you can use a loop to iterate over all these triples instead of having to write each check manually.
#! /bin/bash
n=4
encr=(
".8742cNKlzqQ8Mgjip/Fg1"
"31HvJ8Iinxk2k"
"AyPVCzU.ourSwFdL3N6/YP9RRfIwwKZPNrnt0/yn5vB"
"Cd9AjUI4nGglIcP3MByrZUnu.hHBJc7.eR0o/v0A1gu0/6ztFfBxeJgKTzpgoCLptJS2NnliZLZjO40LUseED/"
)
salt=(bCgxj8Yt 31 klPjs90j 8899Uidd)
algo=(MD5 SHA-256 SHA-512 des)
for candidate in {K,F,L,W}{8,2,9}{x,b,s,v}{'~','-','[','*'}{'$','+',']','%'}; do
for (( i = 0; i < n; i++ )); do
test="$(mkpasswd -m "${algo[i]}" "$candidate" -s "${salt[i]}")"
if [ "$test" = "${encr[i]}" ]; then
echo " ${algo[i]} Salted Hash Password is: $candidate"
echo " Salt: ${salt[i]}"
fi
done
done
Here I removed the duplicated 2 from the 2nd symbol candidates. Thank you Gordon Davisson for pointing this out.
By the way: I used the variable names from your original script. However, it would be better to name the array of hashes something other than encr. Hashing and encryption differ – just like shredding a document and then picking ten of its pieces is different from locking the intact document away.
You can loop over all candidates in a single loop with combinatorial {} expansion:
for candidate in {K,F,L,W}{8,2,9,2}{x,b,s,v}{\~,-,[,\*}{$,+,],%}; do
...
done
Note that this is not POSIX, but works in bash and zsh.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With