You are not logged in.

#1 2022-10-17 11:17:27

MS1
Member
Registered: 2018-02-02
Posts: 84

Count and characterize characters

I found this password entropy calculator and want to make a bash script to count and characterize characters so I can type them into the calculator. I found this for upper and lower case. I modified it for numbers and specials but specials does not work. It just returns the number of characters I type. Is there a better way to do this?

#!/bin/bash

upper=ABCDEFGHIJKMLNOPQRSTUVWXYZ
lower=abcdefghijklmnopqrstuvwxyz
number=0123456789
special='`~!@#$%^&*()-=_+[{]}\|;:",.<>/? '

u=${1//[^$upper]} l=${1//[^$lower]} n=${1//[^$number]} s=${1//[^$special]}
printf 'upper: %d\nlower: %d\nnumber: %d\nspecial: %d\n' "${#u}" "${#l}" "${#n}" "${#s}"
$ bash count.sh ABCabc123@
upper: 3
lower: 3
number: 3
special: 10

Offline

#2 2022-10-17 11:42:49

frostschutz
Member
Registered: 2013-11-15
Posts: 1,417

Re: Count and characterize characters

you could use tr

upper=$(printf "%s" "$pass" | tr --delete --complement A-Z)
echo "A-Z: ${upper} (${#upper})"

lower=$(printf "%s" "$pass" | tr --delete --complement a-z)
echo "a-z: ${lower} (${#lower})"

digit=$(printf "%s" "$pass" | tr --delete --complement 0-9)
echo "0-9: ${digit} (${#digit})"

other=$(printf "%s" "$pass" | tr --delete A-Za-z0-9)
echo "other: ${other} (${#other})"

sample output

A-Z: XCIN (4)
a-z: ajuzx (5)
0-9: 74 (2)
other: *@\,@ (5)

your problem with special is that you do not escape properly for use within [^…]. inside [], the characters with special meanings like \ ^ - ] need escaping

you could also skip filtering the specials altogether. once you know the numbers of the other classes, you can simply substract these from the total length of password

filtering is only useful if you actually interested in the results, to print for verification

Last edited by frostschutz (2022-10-17 11:47:51)

Offline

Board footer

Powered by FluxBB