I tried to generate very big amounts (> 1GB) of pseudo-random data using crypto.randomBytes()
method but I could not produce the exception for drained entropy sources to see what is the behaviour of my application in case of this possible exception.
From Node.JS docs:
Note: Will throw error or invoke callback with error, if there is not enough accumulated entropy to generate cryptographically strong data.
My question is:
How to drain all entropy sources to make crypto.randomBytes()
to produce an exception?
Short answer is - you can't.
Little bit longer answer is - it depends on OS. I assume you use Linux. In theory entropy pool in linux can be easily drained using following script:
#!/bin/bash
while true; do
# write how much entropy is left
cat /proc/sys/kernel/random/entropy_avail
# drain a little bit
dd if=/dev/random of=/dev/null bs=1 count=1 2> /dev/null
done
Running this script will eventually block operations which uses /dev/random
, but not /dev/urandom
. Urandom doesn't read directly from entropy pool, it uses PRNG and reseeds it (by default) every 60 seconds using /dev/random
. So what happen when entropy pool dries up? Nothing. PRNG will be not reseeded, but it will be still generating new numbers, just less cryptographically strong ones.
The only time when this exception could be throwed, is right after system was booted for the first time. I guess it's rather unlikely... Of course other operating systems can handle this matter differently, but as long you use Linux, you shouldn't have to worry about that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With