Password validation via entropy without making users suffer

Password validation via entropy without making users suffer

Password strength validation for normal people: no silly rules, by entropy instead of checkboxes for length, uppercase, digits and special characters. "Minimum 8 characters, lowercase and uppercase letter, digits, special character" — that's not security, that's bureaucracy, like filling out a government form. The password aaaaaA1! technically passes, while "LoremIpsum LoremIpsum LoremLorem IpsumIpsum" doesn't. So instead, I calculate entropy: it measures the actual difficulty of brute-forcing the characters, not just ticking boxes for security theater. A long simple password can be stronger than a short "complex" one. Bonus — a funny error message that makes rejection a little less painful.

How to calculate password entropy?

Easy — just count how many attempts are needed to brute-force the password! It's the number of possible characters (base) raised to the power of the length (exponent). That's it. Well, almost. Why? Because if the password is "aaaaaa", it formally has 6 characters, but in reality it's just one character repeated six times. So we need to account not only for length but also for character diversity. Let's get to it.

Step 1: Determine the base

Let's count which character classes are present in the password:

ClassAdds to base
Lowercase letters (a-z)+26
Uppercase letters (A-Z)+26
Digits (0-9)+10
Other characters (!@# etc.)+26

If you used lowercase, uppercase, and digits — base = 62. Only digits — base = 10.

Step 2: Exponent adjusted for repetitions and sequences

Each new character counts as 1.0, each predictable one — 0.3. Predictable means a repeat, or one that continues a run of three neighbours: 123, qwe, abc, and the same for the Ukrainian layout and alphabet. What for? To reduce the weight of such characters while still counting them — they do add some complexity, after all.

Exponent = new_count × 1.0 + predictable_count × 0.3

Step 3: Threshold

We set a threshold — a balance between user convenience and security. I chose 10¹², ~40 bits, which is roughly the level where brute-force becomes impractical for online attacks. At the same time, researchers from Microsoft Research and Carleton University estimate that withstanding 10⁶ guesses is enough against online attacks, while offline attacks call for 10¹⁴, so you can adjust the threshold considerably for your own case. If the result (base raised to the exponent) is below the threshold — the password is weak, and the user has to come up with a better one.

Examples of the shortest passwords that pass the check

Password Base NewPredict.Exponent Result
V1@dDra 88 7 0 7 88^7 ≈ 4·10¹³
V1@dV1@dV1@d 88 4 8 6.4 88^6.4 ≈ 3·10¹²
VladVladVladVl 52 4 10 7 52^7 ≈ 10¹²
vladvladvladvladvla264 15 8.5 26^8.5 ≈ 10¹²
1234567890123456789012345678901234561023412.210^12.2 ≈ 2·10¹²
111111111111111111111111111111111111111013712.110^12.1 ≈ 10¹²
horsejump26 9 0 9 26^9 ≈ 5·10¹²

As you can see, the very first password with just 7 characters is the hardest to crack. And the second one — 12 characters — would sail through complexity checks on most websites, yet in my system it barely scrapes by.

What this method doesn't catch

The formula protects against character-by-character brute force and against sequences like 12345 or qwerty, but not against dictionary attacks. According to Pwned Passwords, Password1! has appeared in breaches 585 thousand times, yet the formula lets it through with a wide margin. Even horsejump from the table above has been seen in breaches 154 times. It also misses keyboard zigzags like 1q2w3e4r.

So you also need to check against a list of the most common passwords. And if you need a more serious estimate, there is zxcvbn: it solves the same task differently, looking for words, dates and keyboard rows in the password using built-in dictionaries.

By the way, the US standards institute NIST says the same in its password rules SP 800-63B-4: requiring uppercase letters, digits and special characters is prohibited, and checking the password against a list of common and compromised passwords is mandatory. Just think about it: the standard forbids what most websites demand when you create a password! What a shame.

Even the author of the old rules, Bill Burr of NIST, admitted: "Much of what I did I now regret." The UK's National Cyber Security Centre (NCSC) advises the same: don't require complexity, build a password from three random words instead.

Bonus: a funny error message to make the user pick a stronger password

Error: this password uses Epstein, choose another one

Code example in Go

var funnyNames = []string{
	"Alice", "Bob", "Gizmo", "Noodle", "Pickle", "Wombat", "Bubbles", "Sprocket", "Muffin",
	"Einstein", "Tesla", "Curie", "Newton", "Turing", "Lovelace", "Hawking",
	"Gandalf", "Yoda", "Sherlock", "Pikachu", "Groot", "Dobby", "Epstein",
}

var keyRows = []string{
	"1234567890", "qwertyuiop", "asdfghjkl", "zxcvbnm", "abcdefghijklmnopqrstuvwxyz",
	"йцукенгшщзхї", "фівапролджє", "ячсмитьбю", "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя",
}

// continuesRow reports whether a, b, c are neighbours in a keyboard row or the alphabet.
func continuesRow(a, b, c rune) bool {
	forward := strings.ToLower(string([]rune{a, b, c}))
	backward := strings.ToLower(string([]rune{c, b, a}))
	for _, row := range keyRows {
		if strings.Contains(row, forward) || strings.Contains(row, backward) {
			return true
		}
	}
	return false
}

func checkPasswordStrength(password string) error {
	const minComplexity = 1e12
	var hasLower, hasUpper, hasDigit, hasOther bool
	var exponent float64
	seen := make(map[rune]struct{})
	runes := []rune(password)
	for i, r := range runes {
		switch {
		case unicode.IsLower(r):
			hasLower = true
		case unicode.IsUpper(r):
			hasUpper = true
		case unicode.IsDigit(r):
			hasDigit = true
		default:
			hasOther = true
		}

		_, repeated := seen[r]
		if repeated || (i >= 2 && continuesRow(runes[i-2], runes[i-1], r)) {
			exponent += 0.3
		} else {
			exponent += 1.0
		}
		seen[r] = struct{}{}
	}

	baseComplexity := 0
	if hasLower {
		baseComplexity += 26
	}
	if hasUpper {
		baseComplexity += 26
	}
	if hasDigit {
		baseComplexity += 10
	}
	if hasOther {
		baseComplexity += 26
	}

	result := math.Pow(float64(baseComplexity), exponent)
	if result < minComplexity {
		name := funnyNames[rand.Intn(len(funnyNames))]
		return fmt.Errorf("error, this password uses %s, choose another one", name)
	}
	return nil
}

Try it yourself in the interactive password check demo

Entered passwords are not sent anywhere. Trust me.

P.S. Use a password manager. You only need to remember one password. The rest can be long, complex, and always unique.

P.P.S. The same combination counting, but for order and account numbers: how to make a short ID that's easy to copy without mistakes.

Rate this post

Comments and questions · Google / GitHub / anonymous