djb2 is a classic string hash by Daniel J. Bernstein, famous for being tiny yet effective. It appears in countless C codebases and tutorials as the go-to example of a simple hash. This calculator computes both the standard and xor variants of djb2 in your browser.
How it works
djb2 keeps a single 32-bit accumulator:
- Initialise
hash = 5381. - For each byte
c, updatehash = hash * 33 + c. Many implementations write the multiply as((hash << 5) + hash), which equalshash * 33. - The 32-bit accumulator after the last byte is the hash.
The xor variant is identical except step 2 uses hash = hash * 33 ^ c, replacing the addition with a bitwise XOR.
Example
Hashing the string hello with the classic add variant gives 261238937 (decimal), which is 0x0f923099 in hex. The xor variant produces a different value for the same input.
Notes
Both variants are computed with a correct 32-bit overflow multiply so results match reference C code. The choice of 5381 and 33 is empirical, not derived — they simply distribute real-world strings well. djb2 has no cryptographic strength; keep it to hash tables and quick fingerprints. All computation is local.