Home > Uncategorized > Random String Generation

Random String Generation

Random strings have many uses. For example it is best to use hard to guess passwords in online services and a random string comes very handy. Another usage would be to be used as the “salt” in encryption function.

So how do you get a random string?

One way is to use the services of www.random.org, for example the following link will get you 5 random strings to choose from:

http://www.random.org/strings/?num=5&len=10&digits=on&upperalpha=on&loweralpha=on&unique=on&format=html&rnd=new

If you don’t want to use the net (or can’t) then a useful pseudo-random generator is readily available form /dev/urandom and using the following command you generate a 16 character pseudo-random string that for all everyday purposes is good enough.

tr -dc A-Za-z0-9_ < /dev/urandom | head -c 16 | xargs

If you wish, you can wrap it up in a convenient bash function, as in

  genpasswd() {
    local l=$1
    [ "$l" == "" ] && l=16 
    tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
  }

Off you go now!

Advertisements
Categories: Uncategorized Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: