# How do I stop a login form from revealing which accounts exist?
Source: https://ruststudio.org/community/t/80/how-do-i-stop-a-login-form-from-revealing-which-accounts-exist
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, argon2 0.5.3, Windows 11 (release build)
Category: Help and questions | Kind: FAQ | Tags: security, authentication, passwords | Views: 0

## Short answer
Give the same answer for an unknown user and a wrong password, and do the same amount of work in both cases, by verifying against a dummy hash when the account is missing. Otherwise the message or the response time tells an attacker which names are registered.

## Answer
## Answer
If "no such user" and "wrong password" look different, an attacker can test a list of e-mail addresses and learn who has an account. There are two differences to remove: the **message**, and the **time**. Hashing a password takes noticeable time on purpose, so a request for an unknown user that skips the hash would return much faster than one for a real user.

The fix is to always run one verification. When the account is missing, verify against a dummy hash created once at start-up.

```rust title=src/main.rs
use argon2::{
    password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
    Argon2,
};
use std::collections::HashMap;

struct Auth {
    users: HashMap<String, String>, // name -> PHC hash
    dummy: String,                  // a hash nobody can match, made once at start-up
}

impl Auth {
    fn new() -> Auth {
        let salt = SaltString::generate(&mut OsRng);
        let dummy = Argon2::default().hash_password(b"unused", &salt).unwrap().to_string();
        Auth { users: HashMap::new(), dummy }
    }
    fn add(&mut self, name: &str, password: &str) {
        let salt = SaltString::generate(&mut OsRng);
        let h = Argon2::default().hash_password(password.as_bytes(), &salt).unwrap();
        self.users.insert(name.to_string(), h.to_string());
    }
    /// Same work and same answer whether the account exists or not.
    fn login(&self, name: &str, password: &str) -> Result<(), &'static str> {
        let (stored, exists) = match self.users.get(name) {
            Some(h) => (h.as_str(), true),
            None => (self.dummy.as_str(), false),
        };
        let parsed = PasswordHash::new(stored).unwrap();
        let ok = Argon2::default().verify_password(password.as_bytes(), &parsed).is_ok();
        if ok && exists { Ok(()) } else { Err("invalid user name or password") }
    }
}

fn main() {
    let mut auth = Auth::new();
    auth.add("alice", "correct horse");
    println!("{:?}", auth.login("alice", "correct horse"));
    println!("{:?}", auth.login("alice", "wrong"));
    println!("{:?}", auth.login("nobody", "wrong"));
}
```

```text
Ok(())
Err("invalid user name or password")
Err("invalid user name or password")
```

This program shows that the answers are identical. It does not measure time: measure your own login path, in a release build, to confirm that the two cases take comparable time.

## How to do it
1. Create the dummy hash with the same parameters as real ones, so the work is the same.
2. Look the user up, choose the stored hash or the dummy, verify, then decide.
3. Return one generic message for every failure.
4. Apply the same rule to "forgot password": answer "if that address exists, we sent a message" in every case.
5. Add rate limiting and lockout or delays per account and per address, because enumeration is not the only guessing risk.

## Common mistakes
- Different HTTP status codes or wording for the two cases.
- Skipping the hash for unknown users "to save work".
- Revealing existence on the registration form ("this address is taken"). Where you cannot avoid that, protect the form with rate limits.

## Related questions
### Does this fully hide that an account exists?
Not perfectly. Registration, e-mail delivery and timing noise can still leak it. The goal is to remove the obvious differences and to make the rest slow and costly to probe.

### Does a login limit also help here?
Yes. Limit attempts per account and per address, and slow down repeated failures. That reduces both guessing and the use of the login form to probe for names.

## Sources
- OWASP Authentication Cheat Sheet, "Authentication Responses": https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#authentication-responses
- OWASP Forgot Password Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html
