Welcome to the new SimonCook.org design!

Pulling random data from /dev/random

This is a shell script that will pull 5 random characters from /dev/random and print them out to standard output. It was created as the input component for a random password generator that I am currently writing.

Note on using /dev/random: as usual, using this source for entropy will give you pure randomness, though due to the nature of the random source (see Wikipedia entry on /dev/random) the script may sometimes lock up and fail to generate a complete output, as it has run out of randomness. If you are intending to use this alot, then it is advised to use /dev/urandom that doesn’t block, although that comes at the cost of a small bit of entropy.

#!/bin/bash
# Script for pulling x characters from a random source
#   and prints them out to stdout
# Version 1.0.0
# By Simon Cook (https://simoncook.org)
 
# Fill in the two variables below
#  "entropy" is the source of randomness
#  "chars" is the number of chars we want
entropy=/dev/random
chars=5
 
# Pull x characters from source
i=0
while read -r -n1 char
do
    echo -n "$char"
    (( i++ ))
if [ "$i" -eq "$chars" ]; then
   echo ""
   exit 0
fi
done < "$entropy"

Licensing: This small script is released under the CC-GNU GPL licence.



CC-GNU GPL
.

Categorised as Programming and tagged as , , , , , , .
Useful links: Post permalink, RSS feed for this post
Post a comment or leave a trackback: Trackback URL.

Leave a reply

Your email is never published nor shared. Required fields are marked *

*
*