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

后端 未结 7 1287
梦毁少年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:35

    A number of folks have suggested catting and piping through base64 or uuencode. One issue with this is that you can't control how much data to read (it will continue forever, or until you hit ctrl+c). Another possibility is to use the dd command, which will let you specify how much data to read before exiting. For example, to read 1kb:

    dd if=/dev/urandom bs=1k count=1 2>/dev/null | base64
    

    Another option is to pipe to the strings command which may give more variety in its output (non-printable characters are discarded, any runs of least 4 printable characters [by default] are displayed). The problem with strings is that it displays each "run" on its own line.

    dd if=/dev/urandom bs=1k count=1 2>/dev/null | strings
    

    (of course you can replace the entire command with

    strings /dev/urandom
    

    if you don't want it to ever stop).

    If you want something really funky, try one of:

    cat -v /dev/urandom
    dd if=/dev/urandom bs=1k count=1 2>/dev/null | cat -v
    

提交回复
热议问题