aboutsummaryrefslogtreecommitdiff
path: root/bin/rnds.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/rnds.sh')
-rw-r--r--bin/rnds.sh21
1 files changed, 21 insertions, 0 deletions
diff --git a/bin/rnds.sh b/bin/rnds.sh
new file mode 100644
index 00000000..6b3ac904
--- /dev/null
+++ b/bin/rnds.sh
@@ -0,0 +1,21 @@
+# 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
+dd if="$dev" bs=1 count="$count" 2>/dev/null |
+
+# Run cksum(1) over the read random bytes
+cksum |
+
+# cut(1) the cksum(1) output to only the first field, and print that to stdout
+cut -d' ' -f1