aboutsummaryrefslogtreecommitdiff
path: root/bin/rnds.sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-04-05 20:06:39 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-04-05 20:46:47 +1200
commitc8ab406749124d2e762ad5cf53963070113afd0f (patch)
tree54c9721a06957ebe7098a211eea803b0230c0f5d /bin/rnds.sh
parentHandle POSIX correctness in ~/.bash_profile (diff)
downloaddotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.tar.gz
dotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.zip
Apply runtime shebanging to POSIX shell
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