0

looking for an algorithm that when given a First and a last name, an id is generated such that it consists of purely alphanumeric characters. Also, I would want this to be as short as possible whilst maintaining uniqueness. I was hoping for around 10-12 characters - something that a human could enter.

I have read about suggestions of computing a hash, then simply taking the first n bytes and calling modulus with 36 (the idea is that you have a mapping from 0-35 to the letters a-z 0-9).

Also heard suggestions of maybe truncating and using a higher base to pack more bits into the id.

I guess I could append some encoding of the generation time to the produced id to make it unique but again I need a way for this to be short.

What's your opinion? Are there specific hashing algorithms/truncating methods I should go for? I'll be implementing it in javascript as part of a static html page used as a local webapp.

I am just worried as crypto is hard and I would welcome advice from anyone who thinks they know what they are doing with it.

If it helps the number of ids I expect to make is small - around 4 digits.

Saad Attieh
  • 1,396
  • 3
  • 21
  • 42
  • What do you mean by human-readable? A hash is certainly not human friendly. – plalx Nov 23 '14 at 06:53
  • Take a look at this [SO question](http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery) – Max Brodin Nov 23 '14 at 08:49

1 Answers1

0

One technique would be to just use a combination of the first name and last name, similar to how large companies create email aliases. If you only are creating a few thousand, it wouldn't be hard to work around collisions. These are probably the most human friendly type of id to deal with. For example, Bill Smith would be billsm or something similar.

If you don't want your ids to be easily guessable (though if guessing an id breaks your security model you should probably look into that) then you can go with something like the following (untested javascript pseudocode):

var sequence = 1,
    shardId = 1,
    epoch = 1357027200000;

function nextId() {
  sequence = sequence + 1;
  now = Date.now() - epoch;
  seqId = sequence % 1023
  nextId = now << 15 | shardId << 10 | seqId;

  return (nextId).toString(36);
}
Bill
  • 25,119
  • 8
  • 94
  • 125