genphrase/genphrase

PHP应用的安全密码生成器

v1.2.1 2017-03-06 10:46 UTC

This package is not auto-updated.

Last update: 2024-09-10 21:51:13 UTC


README

Packagist License Build Status

GenPhrase 是一个PHP应用的安全密码生成器。GenPhrase基于passwdqc的pwqgen程序。更多信息请参见 http://www.openwall.com/passwdqc/

GenPhrase可以用来生成安全且易于记忆的随机密码。例如,请参阅 示例

GenPhrase可以使用任意大小的单词列表。密码中的单词将从单词集中随机均匀选择。

GenPhrase有一系列小型安全漏洞赏金。更多详情请参见 GenPhrase 安全漏洞赏金

要求

GenPhrase需要PHP版本5.3或更高版本,并启用BC Math (--enable-bcmath)。如果修改单词(例如,大写),则必须可用mbstring扩展。

HHVM兼容性

已确认HipHop VM v2.3及以后版本支持GenPhrase。较早版本的HHVM也可能兼容。

安装

GenPhrase支持使用Composer安装,但请确保您使用至少Composer版本1.0.0-beta1来安装GenPhrase(在1.0.0-beta1之前,Composer容易受到MITM攻击)

genphrase/genphrase

使用GenPhrase生成密码

默认情况下,GenPhrase使用英语单词(english.lst)生成密码。这些密码将至少具有50位的熵。

GenPhrase目前有两个内置的单词列表:english.lst(默认)和diceware.lst。您可以按需添加/删除/组合单词列表。

有关原始english单词列表的更多信息请参见Openwall: http://cvsweb.openwall.com/cgi/cvsweb.cgi/Owl/packages/passwdqc/passwdqc/wordset_4k.c?rev=1.5;content-type=text%2Fplain

GenPhrase的english单词列表与Openwall单词列表之间的唯一修改是我们将所有单词都改为小写。

注意,截至1.1.0版本,GenPhrase捆绑的Diceware列表是EFF的"长"版本,但不包含包含"-"字符的四个单词(因为该字符是GenPhrase分隔符字符)。有关EFF的Diceware列表的更多信息,请参阅: https://www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases

注意,GenPhrase允许您指定单词之间可能使用的分隔符字符。如果您想指定这些分隔符字符,请确保您只使用唯一的单字节字符。有关设置分隔符字符的更多信息,请参见下面的使用示例。

GenPhrase可以生成哪些类型的密码?

一些示例以演示输出

默认设置下,密码可能如下所示

Alter Berlin Paint meaning

生成具有40位熵的密码

musica$Menu&Quota

具有50位熵、分隔符字符和禁用单词大写的密码

setthenrolegiftdancing

用法

<?php
require '/path/to/library/GenPhrase/Loader.php';
$loader = new GenPhrase\Loader();
$loader->register();
<?php
$gen = new GenPhrase\Password();

// Generate a passphrase using english words and (at least) 50 bits of entropy.
$gen->generate();

// Generate a passphrase using english words and custom amount of entropy.
// Entropy must be between 26.0 and 120.0 bits.
$gen->generate(46);

// Remove the default (english) wordlist. This is because we want to use only
// the Diceware list. If you add a new wordlist, but you do not remove the
// default wordlist, then GenPhrase will combine those wordlists.
$gen->removeWordlist('default');

// Add Diceware wordlist.
// $gen->addWordlist('/path/to/GenPhrase/Wordlists/diceware.lst', 'diceware');
// Or more simply (if you give just a filename, GenPhrase will look this
// filename from "Wordlists" folder automatically):
$gen->addWordlist('diceware.lst', 'diceware');
// When creating Diceware phrases, it is recommended not to capitalize any
// words and not to add separator characters (except space, which gets automatically added). To make that
// happen, we configure GenPhrase a little bit more:
$gen->disableSeparators(true); // No separator characters are inserted (except space)
$gen->disableWordModifier(true); // No words are capitalized or changed to lower case (words are not modified)
echo $gen->generate(65) // This will output six "word" passphrases.

// It is possible to force GenPhrase to always use separator characters
// (whether it "makes sense" or not).
// For example, if you generate a passphrase having 35 bits of entropy,
// with default settings, you would get something like: "word1 word2 word3".
// If you force the usage of separators, you would get something like:
// "word1!word2*word3".
$gen->alwaysUseSeparators(true);
// For possible use cases, see pull request #1.

// Change the separator characters.
$gen->setSeparators('123456789');
// NOTE: separator characters must be unique single-byte characters.
// NOTE: you must not use space as a separator character, because space is
// automatically added when appropriate.
// NOTE: minimum number of separator characters is 1. If there there is only
// one unique separator character, it won't add any entropy to the passphrase
// (passphrase may require extra word and become longer).

// Set character encoding. The encoding is used internally by GenPhrase when
// calling mb_ functions.
$gen->setEncoding('iso-8859-1');
// By default GenPhrase uses utf-8 encoding.

熵是如何计算的?

只要我们在单词列表中只有唯一的元素,并且每个元素被选中的概率相等,我们就可以按以下方式计算每个"元素"(通常是一个单词)的熵: log2(count_of_elements)

如果我们选择,比如说,4个元素,总熵为:4 * log2(count_of_elements)

如果我们选择2个元素和一个分隔符元素:2 * log2(count_of_elements) + log2(count_of_separators)

默认情况下,GenPhrase会随机(50:50的概率)修改一个单词的首字母,将其改为大写或小写(例如,“Apple”变为“apple”,“orange”变为“Orange”等。

从熵的角度来看,这意味着我们实际上将“独特元素数量”翻倍(比如说,我们的单词列表中有一个单词“apple”,因此我们可以得到“apple”或“Apple”):log2(2 * 元素数量)

有什么问题或疑问吗?

请发送邮件至timoh6@gmail.com或使用GitHub。