Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Hash functions in your systems serve very different purposes: HashMap buckets, sharding keys, cache keys, password storage and integrity checks. After an incident where a team used MD5 to store API tokens, how would you set company-wide rules?

I would set rules by purpose, because one word, hash, covers tools with opposite goals. Fast non-cryptographic hashes are for distribution in maps, shards and caches; the SHA-256 family is for integrity; passwords need slow, salted password hashing such as Argon2id, bcrypt or scrypt; and API tokens need HMAC with a secret key or a SHA-256 of a high-entropy random token, never plain MD5 or SHA-1. Shard key hashes are long-lived and must be documented and versioned. The rules are enforced with shared libraries and static analysis, not only wiki pages, and designed for crypto agility so algorithms can be rotated without a rewrite.

Why this question is asked

An incident like MD5-hashed API tokens rarely comes from carelessness alone. It usually means the engineers saw hashing as one tool and picked the one they knew. For a senior leader, the fix is not only to repair that service but to make the right choice obvious for every team. Interviewers ask this to see whether we can separate the categories clearly, explain why each needs a different algorithm, and turn that into standards that are actually followed. In this answer, we will look at what went wrong in the incident, define the categories, set the rules for secrets and for shard keys, and then cover enforcement and crypto agility.

1What Went Wrong With MD5 Tokens

MD5 is a cryptographic hash from 1992 whose collision resistance is broken: researchers can create two different inputs with the same MD5 in seconds. But for stored tokens the bigger problem is speed. MD5 is designed to be fast, billions of hashes per second on a GPU, and it uses no secret. If the token table leaks, an attacker can try candidate tokens offline at that speed. If the tokens were low-entropy, for example derived from user ids or timestamps, they can be recovered quickly. Even with strong random tokens, using MD5 signals that nobody decided what property the hash was supposed to provide.

So the first step after the incident is to rotate every affected token, store the new ones correctly, and then ask why the wrong choice was easy to make. That last question leads to the company-wide rules.

2Separating the Categories of Hashing

The central idea is that hash functions serve different purposes, and the purpose decides the algorithm. For distribution, in HashMap buckets, shard keys or cache keys, we want speed and an even spread, and security does not matter: String.hashCode, Murmur3 or xxHash are right. For integrity, checking that a file or message was not corrupted or changed, we want collision resistance: SHA-256 or SHA-3. For authenticity, proving that a message came from someone holding a key, we want a keyed construction: HMAC-SHA256. For passwords, we want the hash to be deliberately slow and salted: Argon2id, bcrypt or scrypt.

Hashing rules by purpose
PurposeUseNever
HashMap buckets, in-memoryhashCode, Murmur3, xxHashcrypto hashes on hot paths
Shard and partition keysMurmur3 or xxHash, versionedString.hashCode across languages
Cache keysSHA-256 of a canonical key if longMD5 when keys come from users
File and message integritySHA-256 or SHA-3MD5, SHA-1, CRC32 for security
Message authenticityHMAC-SHA256 with a managed keyplain hash of secret + data
PasswordsArgon2id, bcrypt, scryptany fast hash, even SHA-256
API tokensrandom 256-bit token, store HMAC or SHA-256MD5, reversible encoding

3Rules for Passwords and Secrets

Passwords are low-entropy, chosen by humans, so the defence is cost: each guess must be expensive. Password hashing functions take a random salt per user, which defeats precomputed tables, and a work factor, which we tune so one hash takes around tens to hundreds of milliseconds on our hardware. Argon2id is also memory-hard, which makes GPU attacks expensive. In Spring Security we use a DelegatingPasswordEncoder, which stores the algorithm id with each hash.

Java
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
String stored = encoder.encode("correct horse battery staple");
System.out.println(stored.startsWith("{bcrypt}")); // true: the default id; check your version
System.out.println(encoder.matches("correct horse battery staple", stored)); // true

API tokens are different. If we generate them ourselves with 256 bits from SecureRandom, they cannot be guessed, so a slow hash adds latency to every request for no benefit. Storing HMAC-SHA256(token, serverKey), or at least SHA-256(token), lets us look tokens up by their hash, and a leaked table is useless without the key. The rule we write down is: secrets we generate are random and stored as a keyed hash; secrets humans choose go through a password hashing function; nothing secret is ever stored with MD5 or SHA-1.

4Rules for Shard Keys and Long-Lived Hashes

Distribution hashes look harmless, but a shard key hash is effectively a data format. If orders are placed on shards by murmur3_32(customerId) % 1024, every service that routes to those shards must compute exactly the same function, in every language, forever. Changing it means moving data. And String.hashCode is a trap here because it is only defined for Java; a Python or Go service will not match it.

So the standard says: shard and partition hash functions are chosen once, documented with the exact algorithm, seed, byte encoding and endianness, given a version number, and published as a shared library with test vectors, known inputs and expected outputs, that every language implementation must pass. A new version is introduced only with a migration plan.

5Enforcing the Rules Instead of Publishing Them

A wiki page helps people who already know they have a question. The teams who used MD5 did not know. So the rules have to show up where the code is written. We provide a small shared library with clearly named functions such as Tokens.newToken(), Tokens.hashForStorage() and Passwords.encoder(), so the easy path is the right one. We add static analysis: CodeQL, Semgrep or SpotBugs with Find Security Bugs can flag MessageDigest.getInstance("MD5") and "SHA-1" in security contexts, and we fail the build on new findings while tracking existing ones as debt. We add a review checklist item for any new storage of secrets and include the topic in security onboarding.

Finally we audit: search all repositories for weak algorithms, classify each use by purpose, and fix the dangerous ones first. MD5 used as a cache key for internal data is low risk; MD5 protecting secrets is urgent.

Step throughRolling out the rules after the incident
  1. Rotate the affected tokensstore new ones as HMAC
  2. Audit repos for MD5 and SHA-1classify each use by purpose
  3. Ship shared library and encodersthe easy path is correct
  4. Add static analysis to the buildfail on new findings
  5. Fix high-risk uses firstsecrets before cache keys
  6. Review rules yearlycrypto agility in place

6 steps in order. Play them, or step through them one at a time.

6Planning for Crypto Agility

Algorithms age. SHA-1 was standard and is now deprecated, and bcrypt's work factor that was right ten years ago is too low today. Crypto agility means we can change algorithms without rewriting systems. In practice, every stored hash carries an identifier of its algorithm and parameters, as the {bcrypt} or {argon2} prefix does in Spring Security, so old and new hashes can coexist. Passwords are rehashed with the new algorithm on the next successful login. Keys for HMAC have ids and are rotated through a key management service. Because the shared library owns these details, a change of algorithm is a library upgrade rather than a project in every team.

7Follow-up questions

Is SHA-256 fine for passwords if we add a salt?

No. A salt stops precomputed tables, but SHA-256 is still fast, so an attacker with a GPU can try billions of guesses per second against each salted hash. Password hashing functions are slow and tunable on purpose, which is the property passwords need.

Why is MD5 still acceptable anywhere?

For non-security purposes such as a checksum against accidental corruption or a cache key for internal data, its weakness does not matter. But because it is easy to misuse and auditors flag it, many companies simply standardise on a fast non-cryptographic hash or SHA-256 instead, so there is one less exception to explain.

How do you migrate existing MD5 password hashes without forcing resets?

We can wrap them: compute bcrypt(md5hash) for every stored value now, so the table is protected immediately, and mark those rows. At the next login, we verify through the wrapper, then store a fresh Argon2id or bcrypt hash of the plain password.

8Conclusion

In this answer, we saw that the MD5 incident came from treating hashing as one tool. We separated the purposes: fast non-cryptographic hashes for distribution, SHA-256 for integrity, HMAC for authenticity, slow salted password hashing such as Argon2id or bcrypt for passwords, and random tokens stored as a keyed hash. We treated shard key hashes as versioned, documented formats because changing them means migration, enforced the rules through shared libraries and static analysis rather than wiki pages, and planned crypto agility with algorithm identifiers and key rotation.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start by separating purposes, because the root cause of the MD5 incident is that hash was treated as one tool. Fast non-cryptographic hashes like Murmur3 or xxHash are for distribution, HashMap buckets, shard keys and cache keys, where speed matters and security does not.

Then, if they want more

Integrity checks use the SHA-256 family. Anything secret is different. Passwords need a slow, salted password hash such as Argon2id or bcrypt, tuned so each guess is expensive. API tokens should be random 256-bit values from SecureRandom, stored as an HMAC with a server key or at least SHA-256, so a leaked table is useless, and never MD5 or SHA-1.

Shard key hashes get their own rule: they are long-lived because changing them means a data migration, so the algorithm, seed and encoding are documented, versioned and shipped with test vectors for every language.

Then enforcement: a shared library with obvious functions like newToken and a password encoder, static analysis in CI that flags MD5 and SHA-1 in security code, and an audit of existing uses, fixing secrets first.

Finally, crypto agility: stored hashes carry an algorithm id, as Spring's delegating encoder does, passwords are rehashed on login, and keys are rotated through a key management service, so we can change algorithms without rewriting services.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Hash functions in your systems serve very different purposes: HashMap buckets, sharding...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/6dbb30a739/