🛠️ToolsShed

Text Entropy Calculator

Calculate Shannon entropy and bits-per-character of any text.

Start typing to calculate entropy.

Frequently Asked Questions

Code Implementation

import math
import re

def password_entropy(password: str) -> float:
    """Calculate Shannon entropy in bits for a password."""
    pool = 0
    if re.search(r"[a-z]", password): pool += 26
    if re.search(r"[A-Z]", password): pool += 26
    if re.search(r"[0-9]", password): pool += 10
    if re.search(r"[^a-zA-Z0-9]", password): pool += 32  # common special chars
    if pool == 0:
        return 0.0
    return len(password) * math.log2(pool)

def shannon_entropy(text: str) -> float:
    """True Shannon entropy based on character frequency."""
    from collections import Counter
    counts = Counter(text)
    n = len(text)
    return -sum((c / n) * math.log2(c / n) for c in counts.values())

# Examples
print(f"'password' entropy: {password_entropy('password'):.1f} bits")    # ~37.6
print(f"'P@ssw0rd!' entropy: {password_entropy('P@ssw0rd!'):.1f} bits")   # ~52.5
print(f"Random 16-char (all types): {password_entropy('aB3!xK9#mN2@pQ7^'):.1f} bits") # ~104.8

# Strength rating
def strength(bits: float) -> str:
    if bits < 28: return "Very Weak"
    if bits < 36: return "Weak"
    if bits < 60: return "Reasonable"
    if bits < 80: return "Strong"
    return "Very Strong"

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.