Skip to content
Minimal Blog
BlueskyHomepage

OpenBSD Shell Password Generator

OpenBSD, Shell1 min read

This is the code I found online.

genpass.sh
#bin/sh
# copy to /usr/local/bin
# usage: genpass [length]
LENGTH=12
if [ 0 -lt $(($1)) ]; then
LENGTH=$1
fi
# extract enough bytes from the random number generator
dd bs=$((80*$LENGTH)) count=1 if=/dev/random 2>/dev/null | LC_CTYPE=C tr -cd 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz' | cut -b 1-$LENGTH

Alternatively, we can use AI generated code:

genpass_strong.sh
#!/bin/ksh
# Function to generate a secure random password
generate_password() {
local length=$1
local charset="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?~"
while [ ${#password} -lt $length ]; do
rand_char=$(dd if=/dev/urandom bs=1 count=1 2>/dev/null | LC_CTYPE=C tr -dc "$charset")
password="${password}${rand_char}"
done
echo "$password"
}
# Generate a password with a length of 12 characters
password=$(generate_password 12)
echo "Generated password: $password"