joanfabregat/secure-token-generator

使用PHP生成加密安全的字母数字令牌

v1.3 2024-05-14 09:41 UTC

This package is auto-updated.

Last update: 2024-09-14 10:18:18 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

使用PHP 8.2+的random_int()函数在PHP中生成加密安全的字母数字令牌。

安装

该软件包可在Packagist上找到。安装库的推荐方式是通过Composer

composer require joanfabregat/secure-token-generator

使用方法

use JoanFabregat\SecureTokenGenerator\SecureTokenGenerator;

// A simple token
$token = SecureTokenGenerator::generate(16);
echo $token; // will echo a 16 characters long alphanumeric token

// With all the options
$token = SecureTokenGenerator::generate(
    length: 32, 
    allowDigits: true, // 1234567890
    allowSpecialChars: true, // !@#$%^&*()_+{}|:"<>?[];',./
    allowUppercase: true, // ABCDEFGHIJKLMNOPQRSTUVWXYZ
    allowLowercase: true, // abcdefghijklmnopqrstuvwxyz
);
echo $token; // will echo a 32 characters long alphanumeric token with special characters

// An integer token
$token = SecureTokenGenerator::generateInt(6);
echo is_int($token); // will echo true
echo $token; // will echo a 6 digits long integer token (ie. 123456)

许可

该库遵循MIT许可协议(见LICENSE文件)。