Tuesday 25 June 2019

linux - Why does dd only copy 128 bytes from /dev/random when I request more?


I'm trying to understand the output of the dd command. I tried


dd if=/dev/zero of=/dev/null bs=512 count=1


and got (as expected):


 1+0 records in
1+0 records out
512 bytes (512 B) copied, 2e-05 seconds, 26 MB/s

However when I tried


dd if=/dev/random of=/dev/null bs=512 count=1


I got


 0+1 records in
0+1 records out
128 bytes (128 B) copied, 0.00012 seconds, 1.1 MB/s

Why is it only copying 128 bytes?



Answer



You need to use /dev/urandom, or the "unblocking" random source.


/dev/random uses a kind of entropy pool to increase the randomness of the bit source. This method will only return as many random bits/bytes as can be returned based on the entropy pool's state at the time, so if a hardware random number generator is used, this can sometimes be a constant. From the Linux manpage:



The generator also keeps an estimate of the number of bits of noise in the entropy pool. From this entropy pool random numbers are created.



The /dev/urandom file keeps reusing the internal pool as-is to generate a number as long as you need. The side-effect of this is: do not use /dev/urandom for cryptographic purposes, as it is less random than the bits produced by /dev/random. See the manpage link above for details.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...