4 ms·
First example immediately reminded me of: head -c 8 /dev/random | base64 I'm not a fan of doing everything through bash, but reaching for python just to get s
by X6S1x6Okd1st 8y ago
First example immediately reminded me of:
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.
- smadge 8y agoWell, 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()
- shabda 8y agoI 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.)
- chriswitts 8y agoI 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 }