Why Servers Never Store Your Original Password: A Deep Dive into Secure Hashing
This article explains why servers cannot know your original password, covering the principles of password hashing, the difference between cryptographic and non‑cryptographic hash functions, the role of salts, and a Java example using SHA‑256 with salt to securely store passwords.
When resetting an account password, many wonder why the server cannot simply retrieve the original password. The reason is that a secure system never stores passwords in plain text; instead it stores a hash.
Storing passwords in plain text poses severe risks, as database leaks or malicious insiders could expose user credentials. Therefore, passwords are transformed using hash algorithms before being saved.
A hash algorithm (also called a digest function) takes input of any length and produces a fixed‑length, unique hash value. Hashes are one‑way: given the hash, it is computationally infeasible to recover the original data.
Hash algorithms can be divided into two categories:
Cryptographic hash algorithms : provide strong security and resistance to collisions, suitable for high‑security scenarios (e.g., SHA‑2, SHA‑3, SM3, BLAKE2, SipHash).
Non‑cryptographic hash algorithms : faster but vulnerable to attacks, used where security is not a concern (e.g., CRC32, MurmurHash3).
For password storage, a common practice is to combine the password with a random salt and then compute a cryptographic hash. Historically MD5+Salt was used, but MD5 is now considered insecure. Modern recommendations favor algorithms such as SHA‑256+Salt or dedicated password‑hashing functions like bcrypt.
Example Java code using SHA‑256 with a salt:
<code>String password = "123456";
String salt = "1abd1c";
// Create SHA-256 digest object
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update((password + salt).getBytes());
// Compute hash
byte[] result = messageDigest.digest();
// Convert hash to hex string
String hexString = new HexBinaryAdapter().marshal(result);
System.out.println("Original String: " + password);
System.out.println("SHA-256 Hash: " + hexString.toLowerCase());
</code>Output:
<code>Original String: 123456
SHA-256 Hash: 424026bb6e21ba5cda976caed81d15a3be7b1b2accabb79878758289df98cbec
</code>When a user logs in, the server retrieves the stored salt, recomputes the hash with the supplied password, and compares it to the stored hash. If they match, authentication succeeds; otherwise it fails.
Because hash functions are irreversible, the server never knows the original password, eliminating the risk of exposing it even if the hash database is compromised.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.