News for hackers. Stuff that matters.
Re:Random Numbers in Bash by gorgoiler on Monday March 27, 2023 @10:41AM (#35327285)
The go-to source for randomness in Linux is /dev/urandom, is it not?

I’m not sure the best way to make use of it as a source of random decimals — there are surely a dozen ways to do so — but this works:

  printf %d 0x`
    head -c 1000 /dev/urandom
    | shasum 
    | head -c 8
  `
As always, my advice for things like this is that if you’re in need of true random numbers in your shell script then you should probably stop writing whatever it is you are writing as a shell script.
Re:Random Numbers in Bash by masklinn on Monday March 27, 2023 @11:41AM (#35328127)
> The go-to source for randomness in Linux is /dev/urandom, is it not?

AFAIK it's the true source of randomness in any unix.

> I’m not sure the best way to make use of it as a source of random decimals — there are surely a dozen ways to do so — but this works:

`od(1)` will do binary to binary to decimal:

    > head -c4 /dev/urandom | od -An -vtu4
               4119929390                                                

    > head -c4 /dev/urandom | od -An -vtu4
               3540752584                                                

    > head -c4 /dev/urandom | od -An -vtu4
                998126897
Re:Random Numbers in Bash by ary on Monday March 27, 2023 @11:51AM (#35328230)
> As always, my advice for things like this is that if you’re in need of true random numbers in your shell script then you should probably stop writing whatever it is you are writing as a shell script.

The use of "true" here seems ambiguous.

Does this statement mean that you discourage creation of cryptographic applications with shell scripts, you discourage use of shell scripts to orchestrate applications that use /dev/urandom for any reason, or something else entirely?

Re:Random Numbers in Bash by noam_k on Monday March 27, 2023 @12:02PM (#35328363)
"True random" as opposed to "pseudo random", which is not cryptographically secure.
Re:Random Numbers in Bash by jimktrains2 on Monday March 27, 2023 @12:31PM (#35328753)
There are cryptographically secure pseudorandom generators. In fact all sources non-hardware (i.e. /dev/random and /dev/urandom) are psuedo random as are the keystream generators for most algos that use one.
Re:Random Numbers in Bash by noam_k on Monday March 27, 2023 @01:05PM (#35329221)
TIL. Thanks!
Re:Random Numbers in Bash by ary on Monday March 27, 2023 @01:15PM (#35329379)
Yes, I'm aware of that. The commenter seems to be implying that one should stop using a shell script when one needs "true" random, and I'm curious as to why since one can call out to Perl, Python, etc.

The linked gist doesn't appear to be making the case for using its recommendations in cryptographic applications so what I'm ultimately after is what the commenter thinks is being done in the shell that's inappropriate.

Re:Random Numbers in Bash by MattPalmer1086 on Monday March 27, 2023 @01:41PM (#35329785)
Incorrect. I'm not sure what you mean by "true random", and we could get into some interesting philosophical discussion on that.

However, all software random number generators are pseudo random, including cryptographic ones. They are called CSPRNGs, which stands for Cryptographically Secure Pseudo Random Number Generator.

Re:Random Numbers in Bash by chasil on Monday March 27, 2023 @11:54AM (#35328273)
There are several scenarios where a cryptographic-quality random number generator might be needed within the context of a shell script.

My favorite tool for this is "openssl rand" and I can find the manual page on rhel7 with "man rand."

Here are 8 bytes of randomness in base64:

  $ openssl rand -base64 8 
  mGHxYcVOmA8=
When I've got dozens of users to create, this is how I am assigning their initial passwords.

The shell's $RANDOM is somewhat less trustworthy than other sources here. It also does not appear to be POSIX, but comes from ksh88, where it is described as returning a random integer between 0 and 32767 each time it is referenced. If unset in a script, it cannot be used again for the life of the shell.

Re:Random Numbers in Bash by TRiG_Ireland on Monday March 27, 2023 @12:46PM (#35328962)
I use this to create passwords when I need to manually:

    function password_generate { head -c ${1-12} /dev/urandom | base64; }
Re:Random Numbers in Bash by chasil on Monday March 27, 2023 @01:27PM (#35329565)
Sometimes, extreme portability makes us do desperate things.

  # uname -a
  HP-UX prdcrm B.10.20 A 9000/800 862741461 two-user license

  # ls -l /dev/urandom
  /dev/urandom not found

  # openssl rand -base64 8
  v51U1RvSD2Q=
I could run this on VAX VMS if you want.
Re:Random Numbers in Bash by rascul on Monday March 27, 2023 @12:07PM (#35328421)
If I recall correctly, /dev/urandom and /dev/random work the same now.
Re:Random Numbers in Bash by dharmab on Monday March 27, 2023 @01:56PM (#35329965)
That change was reverted, and I do not know if they were re-unified since.

https://lwn.net/Articles/889452/

Pragmatically, they are equivalent for most scripting use cases.

Re:Random Numbers in Bash by tzs on Monday March 27, 2023 @12:11PM (#35328478)
Set RD to $N random bytes in decimal:

  RD=$(dc -e 16i`xxd -c $N -g $N -l $N -p /dev/urandom | tr a-f A-F`p)
Re:Random Numbers in Bash by radiator on Monday March 27, 2023 @12:42PM (#35328901)
Unfortunately, this is neither readable nor particularly concise.
Re:Random Numbers in Bash by hayst4ck on Monday March 27, 2023 @12:13PM (#35328509)
While being overly pedantic (because "not true" random is a spectrum), it is good practice to use random numbers to ensure that a recurring cron job does not run at the same time on all machines.

This goes from good practice to necessary as a cron job uses increasing resources or talks to a centralized service.

Puppet implements this as splay: https://www.puppet.com/docs/puppet/7/configuration.html#spla...

Re:Random Numbers in Bash by dharmab on Monday March 27, 2023 @01:54PM (#35329933)
Systemd offers multiple options for randomized delays for timers, documented at: https://www.freedesktop.org/software/systemd/man/systemd.tim...
Re:Random Numbers in Bash by kevin_thibedeau on Monday March 27, 2023 @02:19PM (#35330232)
You don't need the shell for that. Every modern cron supports randomization internally.