bash 如何 base64 编码 /dev/random 或 /dev/urandom?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1250144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to base64 encode /dev/random or /dev/urandom?
提问by alexanderpas
cat /dev/urandom
is always a fun way to create scrolling characters on your display, but produces too many non-printable characters.
cat /dev/urandom
在显示器上创建滚动字符总是一种有趣的方式,但会产生太多不可打印的字符。
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.
是否有一种简单的方法可以在命令行上对其进行编码,使其所有输出都是可读字符,例如 base64 或 uuencode。
Note that I prefer solutions that require no additional files to be created.
请注意,我更喜欢不需要创建额外文件的解决方案。
回答by Pascal MARTIN
What about something like
怎么样的东西
cat /dev/urandom | base64
Which gives (lots of) stuff like
这给出了(很多)类似的东西
hX6VYoTG6n+suzKhPl35rI+Bsef8FwVKDYlzEJ2i5HLKa38SLLrE9bW9jViSR1PJGsDmNOEgWu+6
HdYm9SsRDcvDlZAdMXAiHBmq6BZXnj0w87YbdMnB0e2fyUY6ZkiHw+A0oNWCnJLME9/6vJUGsnPL
TEw4YI0fX5ZUvItt0skSSmI5EhaZn09gWEBKRjXVoGCOWVlXbOURkOcbemhsF1pGsRE2WKiOSvsr
Xj/5swkAA5csea1TW5mQ1qe7GBls6QBYapkxEMmJxXvatxFWjHVT3lKV0YVR3SI2CxOBePUgWxiL
ZkQccl+PGBWmkD7vW62bu1Lkp8edf7R/E653pi+e4WjLkN2wKl1uBbRroFsT71NzNBalvR/ZkFaa
2I04koI49ijYuqNojN5PoutNAVijyJDA9xMn1Z5UTdUB7LNerWiU64fUl+cgCC1g+nU2IOH7MEbv
gT0Mr5V+XAeLJUJSkFmxqg75U+mnUkpFF2dJiWivjvnuFO+khdjbVYNMD11n4fCQvN9AywzH23uo
03iOY1uv27ENeBfieFxiRwFfEkPDgTyIL3W6zgL0MEvxetk5kc0EJTlhvin7PwD/BtosN2dlfPvw
cjTKbdf43fru+WnFknH4cQq1LzN/foZqp+4FmoLjCvda21+Ckediz5mOhl0Gzuof8AuDFvReF5OU
Or, without the (useless) cat+pipe :
或者,没有(无用的) cat+pipe :
base64 /dev/urandom
(Same kind of output ^^ )
(同种输出^^)
EDIT :you can also user the --wrap
option of base64
, to avoid having "short lines" :
编辑:您还可以--wrap
选择base64
, 以避免出现“短线”:
base64 --wrap=0 /dev/urandom
This will remove wrapping, and you'll get "full-screen" display ^^
这将删除包装,您将获得“全屏”显示^^
回答by Adam Batkin
A number of folks have suggested cat
ting 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:
许多人建议cat
使用base64
或uuencode
。这样做的一个问题是您无法控制要读取的数据量(它将永远持续下去,或者直到您按 ctrl+c 为止)。另一种可能性是使用该dd
命令,它可以让您指定退出前要读取的数据量。例如,读取 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.
另一种选择是通过管道传递给strings
命令,这可能会在其输出中提供更多变化(不可打印的字符被丢弃,任何至少 4 个可打印字符的运行[默认] 都会显示)。问题strings
在于它在自己的行上显示每个“运行”。
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
回答by dmckee --- ex-moderator kitten
So, what is wrong with
那么,有什么问题
cat /dev/urandom | uuencode -
?
?
Fixed after the first attempt didn't actually work... ::sigh::
在第一次尝试后修复并没有实际工作...... ::sigh::
BTW-- Many unix utilities use '-' in place of a filename to mean "use the standard input".
顺便说一句——许多 unix 实用程序使用“-”代替文件名来表示“使用标准输入”。
回答by Aaron J Lang
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:
关于如何对随机数据进行 base64 编码(即cat /dev/urandom | base64
),已经有几个很好的答案。但是,在您的问题正文中,您详细说明了:
... encode [urandom] on the command-line in such a way that all of it's output are readable characters, base64 or uuencode for example.
...在命令行上对 [urandom] 进行编码,使其所有输出都是可读字符,例如 base64 或 uuencode。
Given that you don't actually require parseable base64 and just want it to be readable, I'd suggest
鉴于您实际上并不需要可解析的 base64 并且只希望它可读,我建议
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.
base64
仅输出字母数字字符和两个符号(默认为 + 和 /)。[:graph:]
将匹配任何可打印的非空白 ascii,包括 base64 缺少的许多符号/标点符号。因此使用tr -dC '[:graph:]'
将导致更随机的输出,并具有更好的输入/输出效率。
I often use < /dev/random stdbuf -o0 tr -Cd '[:graph:]' | stdbuf -o0 head --bytes 32
for generating strong passwords.
我经常< /dev/random stdbuf -o0 tr -Cd '[:graph:]' | stdbuf -o0 head --bytes 32
用于生成强密码。
回答by Serg
cat /dev/urandom | tr -dc 'a-zA-Z0-9'
回答by greyfade
You can do more interesting stuff with BASH's FIFO pipes:
你可以用 BASH 的 FIFO 管道做更多有趣的事情:
uuencode <(head -c 200 /dev/urandom | base64 | gzip)