How to base64 encode /dev/random or /dev/urandom?

后端 未结 7 1271
梦毁少年i
梦毁少年i 2020-12-25 10:06

cat /dev/urandom is always a fun way to create scrolling characters on your display, but produces too many non-printable characters.

Is there an easy wa

7条回答
  •  执笔经年
    2020-12-25 10:51

    There are already several good answers on how to base64 encode random data (i.e. cat /dev/urandom | base64). However in the body of your question you elaborate:

    ... encode [urandom] on the command-line in such a way that all of it's output are readable characters, base64 or uuencode for example.

    Given that you don't actually require parseable base64 and just want it to be readable, I'd suggest

    cat /dev/urandom | tr -dC '[:graph:]'
    

    base64 only outputs alphanumeric characters and two symbols (+ and / by default). [:graph:] will match any printable non-whitespace ascii, including many symbols/punctuation-marks that base64 lacks. Therefore using tr -dC '[:graph:]' will result in a more random-looking output, and have better input/output efficiency.

    I often use < /dev/random stdbuf -o0 tr -Cd '[:graph:]' | stdbuf -o0 head --bytes 32 for generating strong passwords.

提交回复
热议问题