Hash Generator · Tutorial

How to create an MD5 cache key from a request

5 min readbeginnerUpdated

You need a short, consistent identifier for something long, like a URL with many parameters, a search query, or a block of text, to use as a cache key, a file name or a lookup column. MD5 is a fast, widely available way to do it. This tutorial creates such a key, explains why normalising input first matters, and when to pick a stronger hash.

What you need

  • The Hash Generator
  • The text you want a key for (the Try it button loads a sample sentence)

Steps

  1. Decide exactly what goes into the key

    The key must change when, and only when, the result would change. For an API response, that's usually the endpoint plus its sorted parameters, such as GET /search?lang=en&q=mugs. Leave out things that don't affect the result, like tracking parameters.

  2. Normalise the text

    Use one consistent format, with the same parameter order, the same case and no stray spaces. q=Mugs&lang=en and lang=en&q=mugs describe the same request but produce different hashes.

  3. Hash it

    Paste the normalised text into the box. The 32-character MD5 appears at once. The sample sentence gives 9e107d9d372bb6826bd81d3542a419d6.

  4. Use the key

    Use the hash as the cache key, file name or column value. Prefixing it with a short namespace makes keys easier to manage, such as search:9e107d9d….

    cache.set('search:9e107d9d372bb6826bd81d3542a419d6', response, { ttl: 300 })
  5. Check for the same result in code

    Your application must compute the same hash. In most languages it's one line, for example hashlib.md5(text.encode('utf-8')).hexdigest() in Python. Compare it with the tool's result to confirm your normalisation and encoding match.

Try it yourself

Opens the Hash Generator with this tutorial's sample loaded.

Try it now

Common mistakes

Hashing unnormalised input
Different parameter orders or extra spaces create different keys for the same data, so the cache rarely hits.
Using MD5 where security matters
If an attacker could benefit from two inputs sharing a key, use SHA-256 instead. Collisions are practical to create for MD5.
Different text encodings
The tool hashes UTF-8. If your code uses another encoding, hashes of non-English text won't match.

Next steps

Go back to the Hash Generator, or keep going with one of these:

FAQ

Could two different inputs get the same MD5 by chance?

By accident it's astronomically unlikely, about one in 2^64 even across billions of keys. It's deliberate collisions that make MD5 unsafe for security.