How can I generate random numbers using AShell (restricted bash)? I am using a BusyBox binary on the device which does not have od or $RANDOM. My device has /dev/urandom and /dev/random.
How can I generate random numbers using AShell (restricted bash)? I am using a
Share
$RANDOMandodare optional features in BusyBox, I assume given your question that they aren’t included in your binary. You mention in a comment that/dev/urandomis present, that’s good, it means what you need to do is retrieve bytes from it in a usable form, and not the much more difficult problem of implementing a random number generator. Note that you should use/dev/urandomand not/dev/random, see Is a rand from /dev/urandom secure for a login key?.If you have
trorsed, you can read bytes from/dev/urandomand discard any byte that isn’t a desirable character. You’ll also need a way to extract a fixed number of bytes from a stream: eitherhead -c(requiringFEATURE_FANCY_HEADto be enabled) ordd(requiringddto be compiled in). The more bytes you discard, the slower this method will be. Still, generating random bytes is usually rather fast in comparison with forking and executing external binaries, so discarding a lot of them isn’t going to hurt much. For example, the following snippet will produce a random number between 0 and 65535:Note that due to buffering,
tris going to process quite a few more bytes than whatddwill end up keeping. BusyBox’strreads a bufferful (at least 512 bytes) at a time, and flushes its output buffer whenever the input buffer is fully processed, so the command above will always read at least 512 bytes from/dev/urandom(and very rarely more since the expected take from 512 input bytes is 20 decimal digits).If you need a unique printable string, just discard non-ASCII characters, and perhaps some annoying punctuation characters:
In this situation, I would seriously consider writing a small, dedicated C program. Here’s one that reads four bytes and outputs the corresponding decimal number. It doesn’t rely on any libc function other than the wrappers for the system calls
readandwrite, so you can get a very small binary. Supporting a variable cap passed as a decimal integer on the command line is left as an exercise; it’ll cost you hundreds of bytes of code (not something you need to worry about if your target is big enough to run Linux).