djb2 Hash Calculator

Compute the classic djb2 string hash function

Ad placeholder (leaderboard)

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:

  1. Initialise hash = 5381.
  2. For each byte c, update hash = hash * 33 + c. Many implementations write the multiply as ((hash << 5) + hash), which equals hash * 33.
  3. 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.

Ad placeholder (rectangle)