Normally, when I need some random passwords (or just a random string), I go to randomkeygen.com, copy the kind of password I need and off I go. But if you're a Linux user, like I am, you don't like leaving the terminal. So, I created a simple shorthand:
You can just save this to
randomkeygen.sh or into
/usr/local/bin/randomkeygen to allow you to call
randomkeygen when you need it.
Personally, I like wrapping it in a function and put it in my
.bash_aliases file. This way, the function is only available to me as normal user:
Put the above snippet into the
~/.bash_aliases file, then either restart the terminal or load the aliases manually:
randomkeygen function can be called wherever you want and it should generate something like this:
Based on randomkeygen.com's code
Even though I got the main idea from a snippet I found on Github, the definitions for the passwords I found in randomkeygen's source code. Also, I replaced the character lists with
tr's character classes, except for the hexadecimal strings, since they are both upper and lowercased letters (which can off-course be translated piping another
tr, but this uses less CPU cycles).
Long story short, the command should generate strings that match the formats of the randomkeygen site.
Is this the best way to generate passwords?
No, probably not. This method reads the output of
/dev/urandom, which generates random noise and only if the noise matches the pattern in
tr it's added to the output. First, using
/dev/random instead could generate a more random result, but the pool should be big enough for it to not become an issue. Also, since a lot of noise is thrown away, it's arguable the results might be biased.
Finally, there are many tools which allow you to generate passwords, like
secpwgen, but having a shortcut to the formats I like, beats them all in my opinion. Also, they probably also use
/dev/urandom under the hood.
Oh, and in my local function I removed the shorter WEP strings, I don't use them anyways.