Password entropy is calculated as length × log₂(charset_size) for random strings or words × log₂(wordlist_size) for passphrases. Higher bit counts indicate greater resistance to brute-force attacks, with 60+ bits generally considered secure for modern systems.
Why Entropy Matters More Than Complexity
Complexity refers to the variety of characters used, but entropy measures the total number of possible combinations. A password like "Password1!" looks complex because it mixes cases and symbols, but its entropy is low because humans follow predictable patterns when creating it. A longer passphrase using random words from a large dictionary often yields higher entropy despite appearing simpler. The goal is to maximize unpredictability. Understanding the math helps you choose between short, complex strings and longer, memorable phrases based on your specific security needs.
Calculating Random Password Entropy
For random passwords, entropy depends on the size of the character set and the length of the password. The formula is length × log₂(charset_size). The charset size is the total number of unique characters allowed. If you allow lowercase letters, uppercase letters, digits, and common symbols, the charset size is typically 94. If you restrict it to lowercase letters and digits only, the size is 36. Using the correct charset size is essential for accurate calculation. A larger charset increases entropy logarithmically, while increasing password length increases entropy linearly.
Consider a 12-character password using the full printable ASCII set (94 characters).
import math
charset_size = 94
length = 12
entropy_bits = length * math.log2(charset_size)
print(f"Entropy: {entropy_bits:.2f} bits")
The output is approximately 78.69 bits. This provides strong security for most accounts. Now compare this to a 12-character password restricted to lowercase letters and digits (36 characters).
import math
charset_size = 36
length = 12
entropy_bits = length * math.log2(charset_size)
print(f"Entropy: {entropy_bits:.2f} bits")
The output is approximately 62.11 bits. Restricting the character set significantly reduces entropy even when the length remains constant. This shows why expanding the allowed character set is an efficient way to boost strength for shorter passwords.
Calculating Diceware Passphrase Entropy
Diceware passphrases rely on selecting random words from a fixed list. The formula is number_of_words × log₂(wordlist_size). The standard EFF wordlist contains 7,776 words. Each word adds approximately 12.93 bits of entropy. Unlike random characters, where each character adds a fixed amount based on the charset, each word in a passphrase adds a consistent block of entropy. This linear relationship makes it easy to scale security by adding more words.
Calculate the entropy for a 4-word passphrase using the EFF wordlist.
import math
wordlist_size = 7776
number_of_words = 4
entropy_bits = number_of_words * math.log2(wordlist_size)
print(f"Entropy: {entropy_bits:.2f} bits")
The output is approximately 51.72 bits. While lower than the 12-character random password, this is often sufficient for many applications. If you increase the passphrase to six words, the entropy rises to about 77.58 bits, surpassing the random password example while remaining easier to memorize and type. This trade-off between length and complexity allows you to balance security requirements with usability.
The Role of Random Number Generators
The quality of the random number generator (RNG) directly impacts the actual entropy achieved. Standard JavaScript functions like Math.random() are not cryptographically secure. They use linear congruential generators that produce predictable sequences. If an attacker observes a few outputs, they can predict future values, effectively reducing the entropy regardless of the calculated bit count. For password generation, use crypto.getRandomValues(). This function utilizes a cryptographically secure pseudorandom number generator (CSPRNG), ensuring true unpredictability.
Using a weak RNG can make a theoretically high-entropy password vulnerable. Even if your math shows 80 bits of entropy, a predictable generator might only provide 30 bits of actual randomness. Always verify that your environment supports crypto.getRandomValues(). Most modern browsers do. This ensures that the randomness matches the calculated entropy, providing reliable security.
Verifying Entropy in Your Browser
You can verify entropy calculations directly in your browser console. This allows you to test different combinations of charset sizes and lengths instantly. By comparing calculated bits against known security thresholds, you can optimize your strategy. For instance, adding two words to a Diceware passphrase often provides better entropy gains than adding two characters to a random password, depending on your preferences for memorability versus typing speed.
Here is a function to compare both methods:
function calculateEntropy(type, length, size) {
let entropy;
if (type === 'random') {
entropy = length * Math.log2(size);
} else if (type === 'diceware') {
entropy = length * Math.log2(size);
}
return entropy.toFixed(2);
}
console.log("Random (12 chars, 94 size):", calculateEntropy('random', 12, 94));
console.log("Diceware (4 words, 7776 size):", calculateEntropy('diceware', 4, 7776));
This code outputs the exact bit counts calculated in previous sections. You can adjust the parameters to see how changes affect strength. Tools like PasswordForge automate this process, displaying real entropy bits next to every generated password. This allows you to instantly verify that your chosen method meets your security requirements without manual calculation. The app uses crypto.getRandomValues() to ensure the randomness matches the calculated entropy, providing a reliable measure of strength for both random strings and Diceware passphrases.