bash 生成随机二进制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1462893/
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
Generating a random binary file
提问by Stephan202
Why did it take 5 minutes to generate a 1 KiB file on my (low-end laptop) system with little load? And how could I generate a random binary file faster?
为什么在我的(低端笔记本电脑)系统上以很小的负载生成 1 KiB 文件需要 5 分钟?以及如何更快地生成随机二进制文件?
$ time dd if=/dev/random of=random-file bs=1 count=1024
1024+0 records in
1024+0 records out
1024 bytes (1.0 kB) copied, 303.266 s, 0.0 kB/s
real 5m3.282s
user 0m0.000s
sys 0m0.004s
$
Notice that dd if=/dev/random of=random-file bs=1024 count=1
doesn't work. It generates a random binary file of random length, on most runs under 50 B. Has anyone an explanation for this too?
请注意,dd if=/dev/random of=random-file bs=1024 count=1
这不起作用。它生成一个随机长度的随机二进制文件,大多数运行在 50 B 以下。有人对此也有解释吗?
回答by Stephan202
That's because on most systems /dev/random
uses random data from the environment, such as static from peripheral devices. The pool of truly random data (entropy) which it uses is very limited. Until more data is available, output blocks.
这是因为在大多数系统上/dev/random
使用来自环境的随机数据,例如来自外围设备的静态数据。它使用的真正随机数据(熵)池非常有限。在有更多数据可用之前,输出块。
Retry your test with /dev/urandom
(notice the u
), and you'll see a significant speedup.
使用/dev/urandom
(注意u
)重试您的测试,您将看到显着的加速。
See Wikipediafor more info. /dev/random
does not always output truly random data, but clearly on your system it does.
有关更多信息,请参阅维基百科。/dev/random
并不总是输出真正的随机数据,但在您的系统上显然是这样。
Example with /dev/urandom
:
示例/dev/urandom
:
$ time dd if=/dev/urandom of=/dev/null bs=1 count=1024
1024+0 records in
1024+0 records out
1024 bytes (1.0 kB) copied, 0.00675739 s, 152 kB/s
real 0m0.011s
user 0m0.000s
sys 0m0.012s
回答by Lance Rushing
Try /dev/urandom
instead:
试试吧/dev/urandom
:
$ time dd if=/dev/urandom of=random-file bs=1 count=1024
From: http://stupefydeveloper.blogspot.com/2007/12/random-vs-urandom.html
来自:http: //stupefydeveloper.blogspot.com/2007/12/random-vs-urandom.html
The main difference between random and urandom is how they are pulling random data from kernel. random always takes data from entropy pool. If the pool is empty, random will block the operation until the pool would be filled enough. urandom will genarate data using SHA(or any other algorithm, MD5 sometimes) algorithm in the case kernel entropy pool is empty. urandom will never block the operation.
random 和 urandom 之间的主要区别在于它们如何从内核中提取随机数据。random 总是从熵池中获取数据。如果池是空的, random 将阻止操作,直到池被填满为止。在内核熵池为空的情况下,urandom 将使用 SHA(或任何其他算法,有时是 MD5)算法生成数据。urandom 永远不会阻塞操作。
回答by Bruno Bronosky
I wrote a script to test various hashing functions speeds. For this I wanted files of "random" data, and I didn't want to use the same file twice so that none of the functions had a kernel cache advantage over the other. I found that both /dev/random and /dev/urandom were painfully slow. I chose to use dd to copy data of my hard disk starting at random offsets. I would NEVER suggest using this if you are doing anythings security related, but if all you need is noise it doesn't matter where you get it. On a Mac use something like /dev/disk0 on Linux use /dev/sda
我写了一个脚本来测试各种散列函数的速度。为此,我想要“随机”数据的文件,并且我不想两次使用同一个文件,因此没有一个函数比另一个函数具有内核缓存优势。我发现 /dev/random 和 /dev/urandom 都非常缓慢。我选择使用 dd 从随机偏移量开始复制硬盘数据。如果您正在做任何与安全相关的事情,我永远不会建议使用它,但是如果您只需要噪音,那么从哪里获得它并不重要。在 Mac 上使用 /dev/disk0 之类的东西,在 Linux 上使用 /dev/sda
Here is the complete test script:
这是完整的测试脚本:
tests=3
kilobytes=102400
commands=(md5 shasum)
count=0
test_num=0
time_file=/tmp/time.out
file_base=/tmp/rand
while [[ test_num -lt tests ]]; do
((test_num++))
for cmd in "${commands[@]}"; do
((count++))
file=$file_base$count
touch $file
# slowest
#/usr/bin/time dd if=/dev/random of=$file bs=1024 count=$kilobytes >/dev/null 2>$time_file
# slow
#/usr/bin/time dd if=/dev/urandom of=$file bs=1024 count=$kilobytes >/dev/null 2>$time_file
# less slow
/usr/bin/time sudo dd if=/dev/disk0 skip=$(($RANDOM*4096)) of=$file bs=1024 count=$kilobytes >/dev/null 2>$time_file
echo "dd took $(tail -n1 $time_file | awk '{print }') seconds"
echo -n "$(printf "%7s" $cmd)ing $file: "
/usr/bin/time $cmd $file >/dev/null
rm $file
done
done
Here is the "less slow" /dev/disk0 results:
这是“较慢”的 /dev/disk0 结果:
dd took 6.49 seconds
md5ing /tmp/rand1: 0.45 real 0.29 user 0.15 sys
dd took 7.42 seconds
shasuming /tmp/rand2: 0.93 real 0.48 user 0.10 sys
dd took 6.82 seconds
md5ing /tmp/rand3: 0.45 real 0.29 user 0.15 sys
dd took 7.05 seconds
shasuming /tmp/rand4: 0.93 real 0.48 user 0.10 sys
dd took 6.53 seconds
md5ing /tmp/rand5: 0.45 real 0.29 user 0.15 sys
dd took 7.70 seconds
shasuming /tmp/rand6: 0.92 real 0.49 user 0.10 sys
Here are the "slow" /dev/urandom results:
这是“缓慢”的 /dev/urandom 结果:
dd took 12.80 seconds
md5ing /tmp/rand1: 0.45 real 0.29 user 0.15 sys
dd took 13.00 seconds
shasuming /tmp/rand2: 0.58 real 0.48 user 0.09 sys
dd took 12.86 seconds
md5ing /tmp/rand3: 0.45 real 0.29 user 0.15 sys
dd took 13.18 seconds
shasuming /tmp/rand4: 0.59 real 0.48 user 0.10 sys
dd took 12.87 seconds
md5ing /tmp/rand5: 0.45 real 0.29 user 0.15 sys
dd took 13.47 seconds
shasuming /tmp/rand6: 0.58 real 0.48 user 0.09 sys
Here is are the "slowest" /dev/random results:
这是“最慢”的 /dev/random 结果:
dd took 13.07 seconds
md5ing /tmp/rand1: 0.47 real 0.29 user 0.15 sys
dd took 13.03 seconds
shasuming /tmp/rand2: 0.70 real 0.49 user 0.10 sys
dd took 13.12 seconds
md5ing /tmp/rand3: 0.47 real 0.29 user 0.15 sys
dd took 13.19 seconds
shasuming /tmp/rand4: 0.59 real 0.48 user 0.10 sys
dd took 12.96 seconds
md5ing /tmp/rand5: 0.45 real 0.29 user 0.15 sys
dd took 12.84 seconds
shasuming /tmp/rand6: 0.59 real 0.48 user 0.09 sys
You'll notice that /dev/random and /dev/urandom were not much different in speed. However, /dev/disk0 took 1/2 the time.
您会注意到 /dev/random 和 /dev/urandom 在速度上没有太大区别。但是, /dev/disk0 花费了 1/2 的时间。
PS. I lessen the number of tests and removed all but 2 commands for the sake of "brevity" (not that I succeeded in being brief).
附注。为了“简洁”,我减少了测试次数并删除了除 2 个命令之外的所有命令(不是我成功地简短)。
回答by Mark
Old thread, but like Tobbe mentioned, I needed something like this only better (faster).
旧线程,但就像 Tobbe 提到的那样,我只需要更好(更快)这样的东西。
So... a shell way of doing it the same, just way quicker then random/urandom, useful when creating really big files, I admit not fully random, but close enough probably, depends on your needs.
所以......做同样的事情的shell方式,只是比随机/urandom更快,在创建非常大的文件时很有用,我承认不是完全随机的,但可能足够接近,取决于你的需要。
dd if=/dev/mem of=test1G.bin bs=1M count=1024
touch test100G.bin
seq 1 100 | xargs -Inone cat test1G.bin >> test100G.bin
This will create a 100Gb file from the contents of your ram (the first 1GB, I assume you have so much ram :) ) Note that it's also probably unsafe to share this file since it may contain all kinds of sensitive data like your passwords, so use it only for your own causes :) Oh, and you need to run it as root for the very same reason.
这将从您的 ram 的内容中创建一个 100Gb 的文件(第一个 1GB,我假设您有很多 ram :))请注意,共享此文件也可能不安全,因为它可能包含各种敏感数据,例如您的密码,所以仅将它用于您自己的原因:) 哦,出于同样的原因,您需要以 root 身份运行它。
回答by Tobbe
Old thread but i just needed the same thing. Old friend C came to rescue since i don't want to mess around with scripts. Here is my solution which is good and quick enough for me:
旧线程,但我只需要同样的东西。老朋友 C 来救我了,因为我不想乱搞脚本。这是我的解决方案,它对我来说既好又快:
// usage: ./program <outfile> <size-in-bytes>
#include <stdio.h>
void main(int argc, char** argv){
long long i, s;
FILE* f = fopen(*(argv+1), "w");
srand(time(NULL));
sscanf(*(argv+2), "%lld", &s);
for(i=0;i<s;i++){
fputc(rand()%255,f);
}
fclose(f);
}