DJB2 hash calculator — hash × 33 + c, with the djb2a XOR variant

DJB2 starts at 5381 and, for every byte, multiplies by 33 and adds the byte; type a string to see the unsigned 32-bit result in hex and decimal, or switch to the XOR form known as djb2a.

Live demo · real result from this tool
Variant
DJB2 (hex)34cc38de
Decimal
885799134
Input size
43 bytes

djb2 is a fast non-cryptographic hash for hash tables, caches and deduplication. It is not a checksum with error-detection guarantees (use CRC-32 for that) and not a security hash — anyone can forge data with a chosen value.

Processed on your device — nothing is uploaded.

How to use the DJB2 hash calculator

  1. Type or paste the string in Text.
  2. Leave Input is on Text (UTF-8), or pick Hex bytes or Base64 bytes for raw data.
  3. Choose the Variant: hash × 33 + c (classic djb2) or hash × 33 ^ c (djb2a).
  4. Read DJB2 (hex), the unsigned Decimal value and the Input size.

What it does and when to use it

Daniel J. Bernstein posted this hash to the comp.lang.c newsgroup, and it became one of the most copied snippets in C. You still find it in game engines, embedded firmware, interpreters and coding exercises. Use this page when you need to reproduce a value your code stores, build a test case, or understand why two keys landed in the same bucket.

To compare djb2 with sdbm, FNV, MurmurHash3 and others in one table, use the simple string hash table. For a stronger fast hash, try the FNV hash calculator. To check data for transmission errors, use a real checksum such as CRC-32.

How it works

The tool encodes text as UTF-8 and then processes it one byte at a time:

  • djb2: hash = hash × 33 + byte
  • djb2a: hash = (hash × 33) XOR byte

The starting value is 5381. After every step the result is reduced to an unsigned 32-bit number, so it always fits in 8 hex digits. The code runs in your browser.

Worked examples

One letter. For a (byte 97): 5381 × 33 + 97 = 177670. The tool shows DJB2 (hex): 0002b606 and Decimal: 177670.

Two letters. For ab, the next step is 177670 × 33 + 98 = 5863208, shown as 00597728.

The XOR variant. With djb2a, a gives (5381 × 33) XOR 97 = 177573 XOR 97 = 177604. Then 177604 × 33 = 5860932, and 5860932 XOR 98 = 5860902. So ab returns Decimal: 5860902. The two variants already disagree after the first byte, which is why you must know which one a program uses.

Empty input. No bytes means no steps, so the result is the seed, 5381.

Limits and tips

  • Short strings give small numbers: anything under 4 bytes cannot use the full 32-bit range.
  • Swapping two letters changes the result, because each byte is multiplied by a different power of 33, but collisions are still easy to find on purpose.
  • When you use the hash to pick a bucket, take the decimal value modulo the table size.
  • djb2 is a lookup hash, not an error-detection code; the note under the result points to CRC-32 for that.

Frequently asked questions

Why 5381 and 33?
Both were chosen by experiment. Multiplying by 33 is fast because it is a shift by 5 plus one addition, and in Bernstein's tests these values spread typical text keys well. There is no deeper proof behind them.
My C program gives a different number for long strings. Why?
The original code uses unsigned long, which is 64 bits on most Linux and macOS systems. Once the value passes 2³², the results diverge. This tool wraps at 32 bits, which matches uint32_t versions and most JavaScript ports. Strings of up to 3 bytes agree either way.
Is djb2 good enough for a hash table?
For internal data, yes; it is simple and fast. For keys that outsiders control, such as HTTP parameters, collisions are easy to create on purpose, so use a keyed hash like SipHash there.
Can I use djb2 to check passwords or file integrity?
No. It has no resistance to deliberate collisions and only 32 bits of output. Use SHA-256 for integrity and bcrypt or Argon2 for passwords.

Sources

  1. Hash functions (djb2 and sdbm) — Ozan Yigit, York University, accessed 2026-10-07

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close