Shannon Entropy Scores Password Strength
Count character frequencies and sum -p log2 p to score repeat-heavy strings near zero bits per character — a rough signal, not a security verdict.
Code
import java.util.HashMap;
import java.util.Map;
public class EntropyMeter {
// Shannon entropy of character frequencies, in bits per character;
// a rough strength signal, not a security verdict
static double entropy(String password) {
if (password.isEmpty()) {
return 0.0;
}
Map<Character, Integer> counts = new HashMap<>();
for (char c : password.toCharArray()) {
counts.merge(c, 1, Integer::sum);
}
double bits = 0.0;
for (int count : counts.values()) {
double p = (double) count / password.length();
bits -= p * (Math.log(p) / Math.log(2));
}
return bits;
}
public static void main(String[] args) {
for (String password : new String[] {"aaaaaaaa", "aaaabbbb", "abcdefgh"}) {
System.out.printf("%-8s -> %.3f bits per char%n", password, entropy(password));
}
}
}How the pieces connect — Shannon Entropy Scores Password Strength
Under the hood: the class EntropyMeter and the functions entropy and main.
Iteration happens inside for (char c : password.toCharArray()) — everything outside it runs once.
Watch out for
- Copy, run: the standard library answers every call in the file.
Putting it to use
The entry-point signature is static double entropy(String password)
Start the program and main runs entropy one after another.
Core lines
Map<Character, Integer> counts = new HashMap<>();
double p = (double) count / password.length();
bits -= p * (Math.log(p) / Math.log(2));
Interactive Tools for This Topic
Password Strength MeterScore a password from 0 to 100 based on length and character classes, and get clear suggestions to make it stronger. Everything runs locally.Password Entropy EstimatorEstimate a password's alphabet size and Shannon entropy in bits, then translate that into offline crack times at realistic guessing rates.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.
FAQ
- How do you run Shannon Entropy Scores Password Strength?
- Execute it straight away — the program needs nothing beyond the runtime.
Report a problem with this exampleOpens a pre-filled issue on GitHub.