I spent the weekend majorly re-factoring XKpasswd.pm, my Perl random password generation library. V0.1 was the last thing I wrote before reading Perl Best Practices, and looking back on that code really illustrated the value of that book when used in combination with the perlcritic code analyser.

The new version of the module provides all the functionality the old one did, and more. The refactoring has made the module simpler to use from within scripts, as well as easier to modify and extend. Some new features have also been added, including the ability to use the www.random.org web service as the source of randomness for the library. A full list of bug fixes and new features is included below.

I had hoped to distribute this version as both a ZIP file and a .PKG file, but XCode 4.4 is not being cooperative on the new Mountain Lion, so that will probably have to wait until version 0.3.

Update – 6 August 2012: The link below has been updated to point to version 0.2.1 of the code. Details of the bugs fixed in the release notes.


Installation Instructions

Note: I have only tested the module on OS X and Linux, but provided you have a working Perl environment on Windows it should work there too.

The download file contains three files, copy these to any folder on your system you like. I suggest /usr/local/xkpasswd, and all my examples will assume that location. You may have to chmod 755 example.pl before the sample script works, depending on whether or not your ZIP client preserved the permissions.

That’s it!

Using the Module

The file example.pl included within the zip download is a more full example that’s heavily documented with comments. I’m going to give a much more simplified example here.

Before you can generate a password with the module within a Perl script, you need to do three things, you need to include the library, create a config, and instantiate an XKpasswd object with that config. Then you can call the generate_password() function on that object as often as you like.

To include the module you need to use the use lib directive and point it at the folder you installed the module to before you try to include the module itself with a use directive. Assuming you installed the module in /usr/local/xkpasswd, you would include the following lines at the top of your script:

  1. use lib '/usr/local/xkpasswd';
  2. use XKpasswd;

The config needed to instantiate the object needs to be a hashref, and it MUST include the keys dictionary_file, min_word_length, and max_word_length. You can use as many or as few of the other options are you like (they are all described in example.pl). In this example I’m going to include just a few of the optional keys.

  1. my $xkpasswd_config = {
  2.     dictionary_file => '/usr/local/xkpasswd/sample_dict.txt',
  3.     min_word_length => 4,
  4.     max_word_length => 8,
  5.     num_words => 3,
  6.     custom_separator => '-',
  7.     pad_char => 'RANDOM',
  8. };

Once the config is assembled, you can use it to instantiate an XKpasswd object:

  1. my $xkpasswd = XKpasswd->new($xkpasswd_config);

The line above can cause a fatal exception to be thrown if the config is in some way incorrect, or if there are file permissions preventing the dictionary file from loading. If you want to ‘catch’ this ‘exception’, you can do it like this:

  1. use Carp;
  2. use English qw( -no_match_vars );
  4. my $xkpasswd;
  5. eval{  # try
  6.     $xkpasswd = XKpasswd->new($xkpasswd_config);
  7.     1; # to force the eval to evaluate to true if there were no problems
  8. } or do{ # catch
  9.      print "Failed to create XKpasswd object with error: $EVAL_ERROR\n";
  10. };

When you have instantiated an object you can generate as many password with it as you like in the following way:

  1. my $password = $xkpasswd->generate_password();

This call can also generate a fatal error, especially if you are using the optional link to the www.random.org web service as your source of randomness, so you may also want to surround these calls with eval blocks.

Bugs Fixed in V0.2

  1. You can now use the character 0 (zero) as a custom separator or padding character.

New Features In V0.2

  1. By default the module still uses Perl’s built-in random number generator, but, if you wish, you can use the www.random.org web service instead by setting the random_source config key to the value RANDOM_ORG, AND setting the http_command key to the full path to a command line tool for sending the content of a web page to STDOUT. E.g., on OS X the value ‘/usr/bin/curl -f -s‘ works well.
  2. You can now have no separator at all by setting the config key custom_separator to the value NONE.
  3. A new option for the config key case_transform has been added. If you set it to the value ALTERNATE then each alternate word will be all caps then all lowercase.
  4. The padding character can now be randomly chosen by setting the pad_char config key to the value RANDOM.
  5. In V0.1 the code relied on magic return values to indicate failure, V0.2 does not do this, instead, it uses croak() to ‘throw’ a fatal error with a custom message explaining the error. These fatal errors can be programatically ‘caught’ using the eval or do method shown in the examples above.


2 Responses to “Announcing XKpasswd.pm Version 0.2”

  1. Announcing XKpasswd.pm Version 0.2.1 : Bart Busschots on August 6th, 2012 3:32 pm

    [...] Announcing XKpasswd.pm Version 0.2 [...]

  2. The Tech Lounge Episode 65 on September 21st, 2012 8:00 pm

    [...] 2 of XKpasswd: Improved open soruce library: http://www.bartbusschots.ie/blog/?p=2329 Updated the web interface: http://www.xkpasswd.net OS X Automator action Tutorial: [...]

Leave a Reply

Before you post a comment please remember that commenting on my blog is a privilege not a right. I won't approve comments that are obscene, offensive or insulting. For more info please read this post.

Subscribe without commenting