Skip to content

Commit

Permalink
Increase chunk_size for faster password generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sschmid committed Nov 16, 2024
1 parent 2a11468 commit de735c6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pw
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ pw::gen() {
local -i length=${1:-${PW_GEN_LENGTH}}
local password="" class="${2:-"${PW_GEN_CLASS}"}"

local -i block_size=$(( length / 32 ))
(( block_size == 0 )) && block_size=1

# Fix character classes for BusyBox tr
[[ "${class}" == "[:graph:]" ]] && class="[:alnum:][:punct:]"
[[ "${class}" == "[:print:]" ]] && class="[:alnum:][:punct:][:space:]"

while (( "${#password}" != length )); do
password+=$(dd if=/dev/urandom bs=$(( length / 64 + 1 )) count=1 2>/dev/null | LC_CTYPE=C LC_ALL=C tr -dc "${class}" | head -c $(( length - ${#password} )))
password+=$(dd if=/dev/urandom bs=${block_size} count=1 2>/dev/null | LC_CTYPE=C LC_ALL=C tr -dc "${class}" | head -c $(( length - ${#password} )))
done
pw::output "${password}"
}
Expand Down

0 comments on commit de735c6

Please sign in to comment.