#!/bin/sh
#
# rnds - try to get a low-quality random seed from a random device if possible

# sole optional argument is the bytes to read; 32 is the default
count="${1:-32}"

# try and find a random device to use; none of these are specified by POSIX
for dev in /dev/urandom /dev/arandom /dev/random ''; do
  [ -e "${dev}" ] && break
done

# bail if we couldn't find a random device
[ -n "${dev}" ] || exit 1

# read the bytes from the device
# run cksum(1) over the read random bytes
# cut(1) the cksum(1) output to only the first field, and print that to stdout
dd if="${dev}" bs=1 count="${count}" 2>/dev/null \
  | cksum \
  | cut -d' ' -f1
