Generating short human-readable IDs with error detection
How to generate short IDs that people can read easily, like AKD932-MZR615, with guaranteed no collisions: using a block cipher instead of a hash.
Have you ever had to enter a long code that you can't copy in one go, and you get lost counting a dozen digits? I always wondered if it's possible to make a number that's both short and easy to read. For example, AKD932-MZR615 is easy to copy, although it's quite long. And you can also make the system detect an error if there was one. I had a few ideas on this topic, so I sat down, did the math, and here is the best option I found.
Making a long number easy to read
Patterns are easier to read than a random set of letters and digits. Two or three letters, two or three digits, then again if needed - it seems like an obvious solution. A bonus of the pattern: O and 0 or I and 1 can't be confused, because the position tells whether it's a letter or a digit, and a mistyped character can be fixed automatically without redundant error-correcting codes.
The 12 characters of AKD932-MZR615 hold 26⁶·10⁶ ≈ 3·10¹⁴ combinations — three times as many as 14 digits (10¹⁴). A full alphanumeric encoding with arbitrary positions would give the same number of combinations in 9-10 characters. So overall the proposed pattern reads better than a purely numeric one (hello, awful IBAN) and even needs fewer characters, though more than an arbitrary alphanumeric encoding, which is awful altogether. I use the same combination counting to check password strength via entropy.
| Encoding | Example | Characters | Combinations |
|---|---|---|---|
| Digits only | 82737882637448 | 14 | 10¹⁴ |
| Letter-digit pattern | AKD932-MZR615 | 12 | 26⁶·10⁶ ≈ 3·10¹⁴ |
| Arbitrary letters and digits | X7K2Q-9B4MZ | 10 | 36¹⁰ ≈ 3.7·10¹⁵ |
What to give the user
Random IDs without collisions
Usually people take a random number or a hash a gazillion bits long, and let the users suffer. But we can do better. If the IDs don't need to be unguessable, you don't have to move to N+1-character IDs until all the N-character ones have run out. But how to protect against collisions? Very simple. Certainly not by checking for uniqueness. Just count from one and encode it with any block cipher. That's their defining property - ciphertext can always be unambiguously decrypted, so two different numbers fundamentally cannot give the same result.
You input sequential numbers 0000, 0001, 0002, and get "random", unique, non-repeating numbers 8372, 9274, 1290 as output. Absolutely reliable without collisions, very fast with minimal memory usage - you don't need to store all the IDs. The reverse procedure is similar - take the "random" ID, "decrypt" it and get its sequence number. As long as the counter hasn't reached the maximum, you definitely haven't exhausted all the "random" IDs and you have no collisions.
You just need to choose a block cipher whose block length matches the ID length you want. You can pick a ready-made one, for example FF1, or you can even create an algorithm with that block size - take AES, for example, and modify it. As long as you don't need absolute cryptographic strength, of course. Someone will inevitably shout that you must not roll your own cryptography, but I'll stress it once more: this is only for cases where cryptographic strength doesn't matter. Cryptography can be used not only for security, but also for tasks like ours. Cryptography is simply math, just use it. And modifying AES is quite simple. Issuing "random" IDs without collisions will be just as guaranteed.
Error resistance
You can add a checksum, that is, one character. And even if you get one character wrong when entering it, it will be noticed immediately. And if you add more, you can go further - you can start algorithmically correcting one-character errors, detecting two-character errors, or even more, but that already takes too many check characters. It depends on the cost of an error: for example, mixing up a bank account can be expensive (though here IBAN does well).
IDs resistant to a one-character error...
What if we generate IDs so that a one-character error can't land on another valid ID?
I tried to find ready-made algorithms, but it didn't go well. Well, brute force it is - generate values, and if a one-character error leads to a value already added, we don't add this ID to the list. I brute-forced and brute-forced different patterns - alphanumeric IDs, numeric IDs. And then it hit me. This is the same thing as a one-character checksum. In short - KISS, use a one-character checksum. The irony is that I started writing this blog post while the brute force was running, wanting to share the results of my genius idea. But it turns out this was all figured out long ago. Well, reinventing the wheel is still useful.
Comments and questions · Google / GitHub / anonymous