
Back in 2014 I wrote damm32 to put a Damm check digit on base32 strings, and it now has two new siblings: damm64 for the base64url alphabet and damm93 for the 93 visible ASCII characters other than backslash. Each one has matching C, PHP and JavaScript implementations. The obvious question is which one you should use. The answer depends on who (or what) has to read the code.
A Quick Background on the Damm Algorithm
The Damm algorithm is a fantastic check digit algorithm that appends a single character to the end of a string to catch mistyped characters. It detects every single-character error and every adjacent transposition (two neighbouring characters swapped), which are the two most common transcription mistakes people make. Better still, prepending leading zeros doesn’t change the check digit, so you can use it on fixed-width strings. The only catch is that nearly all existing implementations are base 10 (0 to 9) only – great for checking account numbers, but not much help for alphanumeric strings.
Which Version of the Damm Algorithm Should You Use?
| damm32 | damm64 | damm93 | |
|---|---|---|---|
| Alphabet | Lowercase letters and digits without look-alikes, plus * | base64url | Visible ASCII except \ |
| Bits per character | 5 | 6 | about 6.5 |
| Case | Insensitive | Sensitive | Sensitive |
| Best for | Codes people read and type | IDs and tokens that are copied, or used in URLs | Compact machine-to-machine strings |
The simple rule is if a human types it then use damm32, if a human copies and pastes it then use damm64, and if only machines touch it then use damm93. Each step to the right packs more information into every character, which gives you a shorter string for the same amount of randomness, but makes it harder for a person to read aloud or retype without making a mistake.
Damm64
damm64 uses the base64url alphabet from RFC 4648 section 5, where base64 zero is A and the codes are safe to drop straight into a URL without percent-encoding.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
Input is case-sensitive. Here it is appending a check digit (-a) and then checking for mistakes (-c):
$ damm64 -a Hello Hellop $ damm64 -c Hellop && echo valid valid $ damm64 -c Hellpo || echo invalid invalid $ damm64 -c hellop || echo invalid invalid
The last two examples are an adjacent transposition and a wrong-case letter, and both are caught. The catch is that base64 is full of easily confused characters (0 and O, l and I), so people typing from a page will make mistakes far more often than they would with damm32. damm64 will catch the typo, but your users will curse you for making them retype it – save it for strings that are copied and pasted. If you are checking standard base64 data, strip any = padding first.
Here is the bash one-liner I use to generate a random 22-character URL-safe token (21 random characters plus the check digit):
openssl rand -base64 32 | tr '+/' '-_' | tr -d '=\n' | head -c21 | damm64 -a
Damm93
damm93 uses all the visible ASCII characters (! to ~) except the backslash. The alphabet runs from 0 to ~ (skipping \) and then wraps around from ! to /, so base93 zero is 0 and the digits 0 to 9 keep their face values. Input is case-sensitive, and space, backslash, control characters (tab, newline, DEL) and anything outside ASCII are rejected.
$ damm93 -a World Worldq $ damm93 -a 'x=1;y=2' x=1;y=2L $ damm93 -c Wolrdq || echo invalid invalid
You get about 6.5 bits per character, which gives you the shortest strings of the three, but there are a few things to watch out for:
- The check digit is always visible. Because space is not in the alphabet, trimming whitespace can never accidentally strip off a check digit. The trade-off is that your codes can’t contain spaces.
- There is no backslash. Backslash is the escape character in JSON, C, regular expressions and most shells, so leaving it out avoids the worst escaping headaches. The check digit can still be a quote,
$,&or similar (damm93 -a HellogivesHello"), so remember to quote codes on the command line and escape them properly in SQL and shell scripts. - Damm only guarantees catching single-character errors and adjacent swaps. That is ideal for human transcription errors and truncated strings, but if your main worry is data corrupted in transit, a CRC will catch far more kinds of damage.
How They Work Without a Lookup Table
The Damm algorithm needs a quasigroup (an n by n multiplication table where every symbol appears once in every row and column), but neither library stores a lookup table in memory. Both compute each step from a single formula instead:
damm64: x * y = 2 . (x XOR y) in GF(64), primitive polynomial x^6 + x + 1 damm93: x * y = 2 . (x - y) mod 93
The XOR (in damm64) or subtraction (in damm93) puts zeros along the diagonal, which is why a valid code always reduces to the zero symbol (A for damm64, 0 for damm93). The anti-symmetry that catches every adjacent transposition holds because the multipliers are non-zero in the finite field (damm64) or invertible modulo an odd number (damm93). The modular subtraction formula only works for an odd base, which is why damm93 drops two printable ASCII characters (space and backslash) rather than just one. All 94 visible ASCII characters would make an even base, where 2 is not invertible and the maths falls over.
After the alphabet bug in my original 2014 damm32 code where I accidentally listed z twice (my fault), I made sure the test suites check every quasigroup property exhaustively across all 643 and 933 combinations in all three languages.
Where You Might Use Each Version
damm64 is ideal for tokens and IDs in URLs or strings copied between systems. Invite links, download links and record IDs get copied out of emails and chat windows all the time, and a dropped first or last character is a classic failure mode. If you check the ID at the edge before it ever touches the database, you can tell the user straight away that their link was truncated rather than sending them on a wild goose chase for a missing record.
damm93 is best for internal machine-to-machine strings where no human ever has to read the code aloud. Because there is no space to trim and no backslash to double-escape, it survives being passed through JSON and configuration files much better than a full 94-character ASCII alphabet while giving you the shortest string for a given amount of randomness. Log references and long internal serial numbers are the sort of thing I had in mind.
Both command-line tools are also built for scripts and pipelines. With -c they print nothing and set the exit code, and with no arguments they read codes from stdin, one per line. Put that at the start of a long batch job and you can reject mangled IDs before any real work starts.
Where You Should Not Use Them
A check digit is not security. It catches honest mistakes, not attackers. Anyone can calculate the check digit, so don’t use it to stop someone guessing or forging a token – use a cryptographic signature or MAC for that, and keep the check digit to catch mangled links before you waste time verifying a signature.
Installing and Using the Libraries
Each library installs in a single line (the C version builds with CMake 3.16+, the PHP package needs PHP 7.4+, and the JavaScript package runs on Node.js 20+ or any modern browser):
composer require danieltillett/damm64 npm install damm64 composer require danieltillett/damm93 npm install damm93
The function calls have the same shape in all three languages. Here is the JavaScript version:
import { checkDigit, isValid } from 'damm64';
checkDigit('Hello'); // 'p'
isValid('Hellop'); // trueBoth libraries are licensed under the BSD 2-Clause licence, and the C, PHP and JavaScript test suites are all checked against the same reference test vectors in CI on Linux, macOS and Windows. If you manage to find a string where the three languages disagree, please open an issue on damm64 or damm93 – the tests say it can’t happen, but I have been wrong before.