Search code examples
bashperformancerandomentropy

Seed performance and reliability from urandom


#!/bin/bash

seed1=0
seed2=0

while [[ "$seed1" == 0 || "$seed2" == 0 ]]; do
    seed1=$(head -c 4096 /dev/urandom | tr -dc '0-9' | fold -w 16 | head -n 1)
    seed2=$(head -c 4096 /dev/urandom | tr -dc '0-9' | fold -w 16 | head -n 1)
done

seed_array=($seed1 $seed2)
seed_array=($(echo ${seed_array[*]}| tr " " "\n" | sort -n))

salt_num=[some function id rather not reveal] < /dev/urandom
echo $(echo [seed and salt go here in some order id rather not say] | sha256sum | awk '{print $1}')

I'm creating a seed and a salt for something or other I'm trying to put together, here's a dirty version. now, I like the way this is starting to work, because I'm getting a lot of really good randomness here. But I need to strive for the highest entropy here. I think urandom is the tool for the job, but I don't know how i feel about tr -dc '0-9' right after I pull 4k items from it. it makes it so i have to fold 16 instead of 32, which is what I would prefer. because sometimes, there just aren't enough numbers to choose from.

to be clear, i would like to achieve the following constraints:

  • head -c 4096 as maximum pull from urandom during any one command
  • seed1, seed2 should be a series of 32 ints 0-9 as random as possible
  • if possible, would like to increase fold to 32 reliably (there can never be a seed below a character count of 32)
  • pulling less from urandom is fine. if entropy is preserved.
  • other suggestions are ok if and only if entropy is increased

Like I said before, the tr -dc '0-9' just feels sloppy. and if possible, is there a way to increase the amount of bytes I use from urandom (currently just basically filtering anything that isn't an int already).

it works, but i feel like there must be a better way. anyone with a fresh pair of eyes see something I'm missing?


Solution

  • what about adding | od -An -tx8 between head and tr to convert output in hex so that 0-9 have more likely to occur.