[Catalyst] Confirmations numbers for Humans

Nathan Kurz nate at verse.com
Thu Mar 2 16:57:45 CET 2006


On Thu, Mar 02, 2006 at 07:31:07AM -0800, Bill Moseley wrote:
> I'm curious about those short airline confirmation ids.
> My last airline confirmation number was BU8743, which is a lot easier
> to say over the phone than, say, a UUID or md5 hash.  But, their
> unique-ness is, well, less unique.  I assume those confirmation
> numbers are only valid for some defined period of time.
> 
> Is anyone generating short ids like those airline confirmation
> numbers?
> 
> How are you generating them and what actions do you use to
> keep them unique?  What's your system for deciding when to purge old
> ids and how to recycle them?  If you do purge them, do you have a
> separate way to lookup a transaction after they are purged?
> 
> I suppose to create them I could truncate a MD5 hash and then test the
> database to see if it exists (using a serializable transaction
> isolation level).  I've used other metheds such as creating a random
> hex number and appending a day number sequence.

This isn't directly applicable to your problem, but for a prototype
I'm working on I'm doing something like this:

    # generate a token to be used for confirmation of email address
    my @chars = ('a'..'z', 'A'..'Z', 0..9);
    my $token;
    for my $try (1..5) {
        $token = join '', map $chars[rand @chars], 0..16;
        my $count = Notate::Model::DB::Request->count({token => $token});
        last unless $count;
        $c->log->error("duplicate token '$token' ($try)");
        die "Could not generate a new token" if ($try > 4);
    }

The useful line (grabbed from a web page I've long forgotten) is the
"map $chars[rand @chars], 0..16;", which generates a random string of
characters from your set of @chars of the length your desire (16 in
this case).  If one did a shorter string from a set of chars that are
easy to read aloud, this might work for you.  There are probably
better ways to do all of this, but it's meeting my needs right now.

Nathan Kurz
nate at verse.com



More information about the Catalyst mailing list