跳到内容
🛠️ToolsShed

安全密码短语生成器

生成带有熵估算的 Diceware 风格易记密码短语。

38

关于此工具

密码短语是由互不相关的单词组合而成的易记忆且安全的身份验证凭据。安全密码短语生成器使用Diceware方法生成密码短语,这是一种经过验证的密码学技术,利用人脑记忆单词序列的自然优势,而非依赖随机字符组合。与充满特殊字符的传统密码不同,Diceware密码短语通过简洁性提供卓越的安全性,非常适合长期凭据使用,如密码管理器主密码、银行账户和高价值机密。

生成器的使用非常简单:选择所需的单词数量(通常根据用途选择5到6个词),选择分隔符字符,并根据需要切换首字母大写选项。该工具会立即计算熵——密码强度的统计度量——并以比特形式显示密码短语提供的确切安全级别。每增加一个单词,熵会以对数方式增加;例如,4个单词的密码短语提供约51位熵,而6个单词的密码短语跳升至约77位,超过现代安全建议。

Diceware密码短语之所以有效,是因为它结合了真正的随机性(来自计算机的密码学安全随机数生成器)和大型词表(通常为7,776个词),确保攻击者无法猜测或预测结果。许多系统要求特殊字符或数字;与其用可预测的替换来削弱密码短语,不如在此处生成短语,如果账户需要额外的限制条件,则将其存储在密码管理器中。该工具对开发者、安全专业人员以及重视易记忆性和数学确定性相结合的任何人管理敏感凭据特别有价值。

常见问题

代码实现

import secrets
import math

# A small built-in word list; replace with a full EFF large wordlist in production.
WORDLIST = [
    "apple", "brave", "crane", "drift", "eagle", "flame", "grace", "hover",
    "ivory", "jewel", "knack", "lunar", "maple", "noble", "ocean", "pearl",
    "quilt", "raven", "stone", "tiger", "umbra", "vivid", "waltz", "xenon",
    "yacht", "zonal", "amber", "blaze", "crisp", "delta", "ember", "frost",
    "globe", "haste", "index", "joust", "karma", "lance", "marsh", "nerve",
    "ozone", "pilot", "query", "ridge", "storm", "trove", "ultra", "vigor",
    "width", "xylem", "yearn", "zesty",
]

def generate_passphrase(
    word_count: int = 4,
    separator: str = "-",
    capitalize: bool = False,
    append_number: bool = False,
) -> str:
    words = [secrets.choice(WORDLIST) for _ in range(word_count)]
    if capitalize:
        words = [w.capitalize() for w in words]
    phrase = separator.join(words)
    if append_number:
        phrase += separator + str(secrets.randbelow(9000) + 1000)
    return phrase

def passphrase_entropy(word_count: int, wordlist_size: int) -> float:
    """Bits of entropy: log2(wordlist_size ^ word_count)"""
    return word_count * math.log2(wordlist_size)


if __name__ == "__main__":
    for _ in range(5):
        phrase = generate_passphrase(4, "-", capitalize=True, append_number=True)
        print(phrase)
    bits = passphrase_entropy(4, len(WORDLIST))
    print(f"Entropy with {len(WORDLIST)}-word list, 4 words: {bits:.1f} bits")

Comments & Feedback

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