darkv/php-password-generator

PHP 类 PasswordGenerator 作为密码生成器,可以创建像 macOS ≤ 10.14 的钥匙串那样易于记忆的密码。

2.0.4 2024-05-11 06:58 UTC

This package is auto-updated.

Last update: 2024-09-11 09:50:07 UTC


README

PHP 类 PasswordGenerator 作为密码生成器,可以创建像 macOS ≤ 10.14 的钥匙串那样易于记忆的密码。

入门

通过 composer 添加

使用 composer 将生成器安装到您的项目中

composer require darkv/php-password-generator

直接导入

将文件 PasswordGenerator.php 复制到您的项目中,并在您的 PHP 文件中使用 include 'PasswordGenerator.php'; 包含它。然后通过使用针对特定语言的预定义静态方法或通过使用标准构造函数来自定义它来创建一个实例。

include 'PasswordGenerator.php';

// Import the namespace
use \Darkv\PhpPasswordGenerator\PasswordGenerator;

// create instance with an English word list
$gen = PasswordGenerator::EN();

// generate a password
echo $gen->generate();

先决条件

此类适用于 PHP >= 7.4,并且需要一个可用的互联网连接。

密码语法

生成密码的语法可以通过一个模式来定义。该模式由定义密码字符串结构的控制字符组成。可用的控制字符有:

  • i
    一个介于 1 和 999 之间的整数。
  • s
    一个标点符号字符(ASCII 码 33 到 47)。
  • w
    单词表中的一个单词。

如果您不提供自己的模式,则使用默认模式 wisw。一些使用该默认模式生成的密码示例有:

  • Theyre778+Breakthrough
  • Reforms13)Translated
  • When249*Awards

单词列表

该类使用 RSS 源来构建一个单词列表,从中随机选择单词用于密码生成。该类为英语和德语提供了一些预定义的配置,但也可以进行自定义。

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

// create instance with English word list
$gen = PasswordGenerator::EN();

// create instance with German word list
$gen = PasswordGenerator::DE();

// create instance with custom parameters
$gen = new PasswordGenerator([
    'url'       => 'https://www.tagesschau.de/newsticker.rdf',
    'minLength' => 3,
    'maxLength' => 6,
]);

参数 minLengthmaxLength 表示从 URL 源获取的单词允许的长度。如果成功构建了单词列表,该列表将保存到文件 wordlist.json 中。下次创建 PasswordGenerator 的实例时,如果无法联系 URL 源或该 URL 源不包含任何可用的单词,则将加载该缓存列表。如果您重复使用相同的实例,则也会重用单词列表,因此不会生成进一步的 HTTP 请求。

可选地,您可以指定 wordCacheFile 来控制缓存的单词列表的位置和名称。

向自定义实例传递可选的布尔参数 fetch。当为 false 时,将优先选择缓存的单词列表,但如果无法加载,则会回退到获取。

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

$gen = PasswordGenerator::EN();

// reuse word list without rebuilding
echo 'Password 1: ', $gen->generate();
echo 'Password 2: ', $gen->generate();
echo 'Password 3: ', $gen->generate();

在获取 RSS 源时,默认行为是创建一个新的列表,覆盖现有的缓存文件。您可以选择将新单词列表与现有缓存文件合并,通过将 appendWordlist 参数设置为 true 来选择此选项。一方面,这将导致单词列表更大,增加了熵,另一方面,这也可能导致列表非常长。您可以使用 limitWordlist 配置参数限制单词列表中的项目数量。当使用此选项且单词列表超出该限制时,它将被洗牌并切片到该数量。

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

// append NYTimes feed to the specified wordlist, limiting to 7000 items max
$gen = new PasswordGenerator([
    'wordCacheFile'  => 'mywords.json',
    'url'            => 'https://rss.nytimes.com/services/xml/rss/nyt/World.xml',
    'minLength'      => 3,
    'maxLength'      => 8,
    'appendWordlist' => true,
    'limitWordlist'  => 7000,
]);

缓存

如果您需要在没有互联网连接的环境中使用该类,可以预先构建一个单词列表,并将生成的 wordlist.json 文件复制到您的项目中。当使用 PasswordGenerator 时,您可以告诉它只使用该缓存列表并跳过 URL 源请求。

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

$gen = PasswordGenerator::CACHED();

echo $gen->generate();

缓存文件的存储位置可以作为参数传递给CACHED,默认情况下,工作目录中的wordlist.json将被使用。

URL源

作为单词列表的源,此类使用可配置的RSS源。该源必须是XML格式,并包含description标签,从这些标签中提取文本内容。

HTTP重定向

出于安全考虑,如果您想防止PasswordGenerator在获取给定URL时跟随重定向,可以将参数httpRedirects设置为0。默认为2,最多跟随两个重定向。

示例

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

// create instance with custom parameters
$gen = new PasswordGenerator([
    'url'           => 'https://www.some-url.com/source',
    'minLength'     => 3,
    'maxLength'     => 6,
    'httpRedirects' => 0,
]);

参数

创建PasswordGenerator实例时,您可以提供以下参数

  • url

    用于从其中提取单词的文档的URL。

  • minLength

    单词必须具有的最小字符数才能包含在单词列表中。

  • maxLength

    单词可以具有的最大字符数才能包含在单词列表中。

  • wordCacheFile

    缓存文件的名称。默认为‘wordlist.json’。

  • httpRedirects

    控制在URL访问期间是否以及跟随多少个HTTP重定向。默认为2。要防止跟随重定向,请将其值设置为0

  • appendWordlist

    如果为true,则获取的单词列表将被附加到现有缓存文件中。如果为false,则缓存将被覆盖。

  • limitWordlist

    声明单词列表可能包含的最大单词数量的整数值。当使用appendWordList时很有用。

许可

本项目采用MIT许可协议 - 有关详细信息,请参阅LICENSE.md文件。