Re:Tweetable Python – Tweetable Python 3.7 Documentation
by chriswitts on Wednesday August 08, 2018 @02:22AM
(#17713588)
I have a little function for generating some random characters
######################################
# Generate a random password from /dev/urandom
# pw [length [num_passwords [characters]]
# Arguments:
# $1 => The length of password
# $2 => The number of passwords to generate
# $3 => The acceptable character set, PCRE regex
######################################
function pw {
if [[ "x$1" == "x-h" ]]; then
echo "Usage: pw [length:20 [num_passwords:1 [character_set:a-zA-Z0-9._!@#$%^&*()]]]"
else
LC_CTYPE=C \
tr -dc "${3:-'a-zA-Z0-9._!@#$%^&*()'}" < /dev/urandom \
| fold -w "${1:-20}" \
| head -n "${2:-1}"
fi
# There's always `openssl rand -base64 12` for simplicity
}
Re:Tweetable Python – Tweetable Python 3.7 Documentation
by smadge on Tuesday August 07, 2018 @04:57PM
(#17710730)
Well, if you are writing a python program which needs this functionality, it would probably be better to write it in python instead of:
password = os.popen("head -c 8 /dev/random | base64").read()
Re:Tweetable Python – Tweetable Python 3.7 Documentation
by shabda on Tuesday August 07, 2018 @10:48PM
(#17712846)
I think the Python approach is better :).
> head -c 8 /dev/random | base64 Gives me PRM9hlMDpG0=
Which is 12 chars, not a big deal, but with Python I specified exactly how long the password should be.
With Python, I just used the first example as the anchor and then built more examples. (For example enforce it to be pronounceable by using every second char as vowel, or build a `horse battery staple generator` style password.)
head -c 8 /dev/random | base64
I'm not a fan of doing everything through bash, but reaching for python just to get some random characters seems particularly bad.