如何对 /dev/random 或 /dev/urandom 进行 Base64 编码?
cat /dev/urandom
始终是在显示器上创建滚动字符的有趣方法,但会产生太多不可打印的字符。
有没有一种简单的方法可以在命令行上对其进行编码,使其所有输出都是可读字符,例如 base64 或 uuencode。
请注意,我更喜欢不需要创建其他文件的解决方案。
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 way to encode it on the command-line in such a way that all of its output are readable characters, base64 or uuencode for example.
Note that I prefer solutions that require no additional files to be created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
类似
Which 提供(很多)类似
Or 的东西怎么样,没有(无用的) cat+pipe :
(相同类型的输出 ^^ )
编辑:您还可以使用
base64
的--wrap
选项,以避免出现“短行”:这将删除换行,并且您将会得到“全屏”显示^^
What about something like
Which gives (lots of) stuff like
Or, without the (useless) cat+pipe :
(Same kind of output ^^ )
EDIT : you can also user the
--wrap
option ofbase64
, to avoid having "short lines" :This will remove wrapping, and you'll get "full-screen" display ^^
许多人建议通过
base64
或uuencode
进行cat
编码和管道传输。 这样做的一个问题是您无法控制要读取的数据量(它将永远持续,或者直到您按下 ctrl+c)。 另一种可能性是使用 dd 命令,它可以让您指定退出之前要读取的数据量。 例如,要读取 1kb:另一个选项是通过管道传输到
strings
命令,该命令可能会提供更多种类的输出(不可打印的字符将被丢弃,任何至少 4 个可打印字符的运行 [默认情况下]显示)。strings
的问题是它在自己的行上显示每个“run”。(当然,
如果您不希望它停止,您可以将整个命令替换为)。
如果你想要一些真正时髦的东西,请尝试以下之一:
A number of folks have suggested
cat
ting and piping throughbase64
oruuencode
. 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 thedd
command, which will let you specify how much data to read before exiting. For example, to read 1kb: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 withstrings
is that it displays each "run" on its own line.(of course you can replace the entire command with
if you don't want it to ever stop).
If you want something really funky, try one of:
那么,有什么问题吗
?
在第一次尝试实际上不起作用后修复...::sigh::
BTW——许多 UNIX 实用程序使用“-”代替文件名来表示“使用标准输入”。
So, what is wrong with
?
Fixed after the first attempt didn't actually work... ::sigh::
BTW-- Many unix utilities use '-' in place of a filename to mean "use the standard input".
关于如何对随机数据进行 base64 编码(即
cat /dev/urandom | base64
),已经有几个很好的答案。然而,在您的问题正文中,您详细阐述了:鉴于您实际上并不需要可解析的 base64,只是希望它可读,我建议
base64
仅输出字母数字字符和两个符号(默认情况下为 + 和 /)。[:graph:]
将匹配任何可打印的非空白 ascii,包括 base64 缺少的许多符号/标点符号。因此,使用 tr -dC '[:graph:]' 将产生看起来更随机的输出,并具有更好的输入/输出效率。我经常使用
< /dev/random stdbuf -o0 tr -Cd '[:graph:]' | stdbuf -o0 head --bytes 32 用于生成强密码。
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:Given that you don't actually require parseable base64 and just want it to be readable, I'd suggest
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 usingtr -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.您可以使用 BASH 的 FIFO 管道做更多有趣的事情:
You can do more interesting stuff with BASH's FIFO pipes:
尝试
xxd(1)
Try
xxd(1)