Phalcon 加密
注意
所有类都以前缀命名Phalcon
加密\Crypt¶
-
命名空间
Phalcon\Encryption
-
使用
Phalcon\Encryption\Crypt\CryptInterface
Phalcon\Encryption\Crypt\Exception\Exception
Phalcon\Encryption\Crypt\Exception\Mismatch
Phalcon\Encryption\Crypt\PadFactory
-
继承
-
实现
CryptInterface
为 Phalcon 应用程序提供加密功能。
use Phalcon\Crypt;
$crypt = new Crypt();
$crypt->setCipher("aes-256-ctr");
$key =
"T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3";
$input = "The message to be encrypted";
$encrypted = $crypt->encrypt($input, $key);
echo $crypt->decrypt($encrypted, $key);
常量¶
const DEFAULT_ALGORITHM = sha256;
const DEFAULT_CIPHER = aes-256-cfb;
const PADDING_ANSI_X_923 = 1;
const PADDING_DEFAULT = 0;
const PADDING_ISO_10126 = 3;
const PADDING_ISO_IEC_7816_4 = 4;
const PADDING_PKCS7 = 2;
const PADDING_SPACE = 6;
const PADDING_ZERO = 5;
属性¶
/**
* @var string
*/
protected $authData = ;
/**
* @var string
*/
protected $authTag = ;
/**
* @var int
*/
protected $authTagLength = 16;
/**
* Available cipher methods.
*
* @var array
*/
protected $availableCiphers;
/**
* @var string
*/
protected $cipher;
/**
* The name of hashing algorithm.
*
* @var string
*/
protected $hashAlgorithm;
/**
* The cipher iv length.
*
* @var int
*/
protected $ivLength = 16;
/**
* @var string
*/
protected $key = ;
/**
* @var int
*/
protected $padding = ;
/**
* @var PadFactory
*/
protected $padFactory;
/**
* Whether calculating message digest enabled or not.
*
* @var bool
*/
protected $useSigning = true;
方法¶
public function __construct( string $cipher = static-constant-access, bool $useSigning = bool, PadFactory $padFactory = null );
$encrypted = $crypt->decrypt(
$encrypted,
"T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
);
$encrypted = $crypt->encrypt(
"Top secret",
"T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
);
The $key
应该已在加密安全方式下生成。
错误的密钥:"le password"
更好(但仍不安全)-> "#1dj8$=dp?.ak//j1V$~%*0X"
好的密钥:"T4\xb1\x8d\xa9\x98\x05\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3"
更改使用的填充方案。 设置是否使用计算消息摘要。 检查某个密码或哈希算法是否可用protected function cryptPadText( string $input, string $mode, int $blockSize, int $paddingType ): string;
protected function cryptUnpadText( string $input, string $mode, int $blockSize, int $paddingType ): string;
如果函数检测到文本没有被填充,它将原样返回。
protected function decryptGcmCcmAuth( string $mode, string $cipherText, string $decryptKey, string $iv ): string;
protected function encryptGcmCcm( string $mode, string $padded, string $encryptKey, string $iv ): string;
加密\Crypt\CryptInterface
¶
-
命名空间
Phalcon\Encryption\Crypt
-
使用
-
继承
-
实现
Phalcon\Crypt 的接口
方法¶
解密一段文本 解码一个以 base64 字符串编码的文本 加密一段文本 加密一段文本并返回结果为 base64 字符串 返回认证数据 返回认证标签 返回认证标签长度 返回可用的密码列表 返回当前密码 返回加密密钥 设置认证数据 设置认证标签 设置认证标签长度 设置密码算法 设置加密密钥 更改使用的填充方案。 设置是否使用计算消息摘要。加密\Crypt\异常\Exception¶
-
命名空间
Phalcon\Encryption\Crypt\Exception
-
使用
-
继承
\Exception
-
实现
Phalcon\Crypt 中抛出的异常使用此类
加密\Crypt\异常\不匹配¶
-
命名空间
Phalcon\Encryption\Crypt\Exception
-
使用
-
继承
Exception
-
实现
Phalcon\Crypt 中抛出的异常将使用此类。
加密\Crypt\填充工厂¶
-
命名空间
Phalcon\Encryption\Crypt
-
使用
Phalcon\Encryption\Crypt
Phalcon\Encryption\Crypt\Padding\PadInterface
Phalcon\Factory\AbstractFactory
Phalcon\Support\Helper\Arr\Get
-
继承
AbstractFactory
-
实现
PadFactory 类
@package Phalcon\Crypt
属性¶
方法¶
AdapterFactory构造函数。 创建适配器的新实例 获取 Crypt 填充常量并返回填充类的唯一服务名称加密\Crypt\填充\Ansi¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
Ansi 类
@package Phalcon\加密\Crypt\填充
方法¶
加密\Crypt\填充\Iso10126¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
Iso10126 类
@package Phalcon\加密\Crypt\填充
方法¶
加密\Crypt\填充\IsoIek¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
IsoIek 类
@package Phalcon\加密\Crypt\填充
方法¶
加密\Crypt\填充\Noop¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
类 Noop
@package Phalcon\加密\Crypt\填充
方法¶
加密\Crypt\填充\PadInterface
¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
Phalcon\加密\Crypt\填充 的接口
方法¶
加密\Crypt\填充\Pkcs7¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
Pkcs7 类
@package Phalcon\加密\Crypt\填充
方法¶
加密\Crypt\填充\Space¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
Space 类
@package Phalcon\加密\Crypt\填充
方法¶
加密\Crypt\填充\Zero¶
-
命名空间
Phalcon\Encryption\Crypt\Padding
-
使用
-
继承
-
实现
PadInterface
Zero 类
@package Phalcon\加密\Crypt\填充
方法¶
加密\Security¶
-
命名空间
Phalcon\Encryption
-
使用
Phalcon\Di\AbstractInjectionAware
Phalcon\Di\DiInterface
Phalcon\Encryption\Security\Exception
Phalcon\Encryption\Security\Random
Phalcon\Http\RequestInterface
Phalcon\Session\ManagerInterface
-
继承
AbstractInjectionAware
-
实现
此组件提供一组函数来提高 Phalcon 应用程序的安全性
$login = $this->request->getPost("login");
$password = $this->request->getPost("password");
$user = Users::findFirstByLogin($login);
if ($user) {
if ($this->security->checkHash($password, $user->password)) {
// The password is valid
}
}
常量¶
const CRYPT_ARGON2I = 10;
const CRYPT_ARGON2ID = 11;
const CRYPT_BCRYPT = 0;
const CRYPT_BLOWFISH = 4;
const CRYPT_BLOWFISH_A = 5;
const CRYPT_BLOWFISH_X = 6;
const CRYPT_BLOWFISH_Y = 7;
const CRYPT_DEFAULT = 0;
const CRYPT_EXT_DES = 2;
const CRYPT_MD5 = 3;
const CRYPT_SHA256 = 8;
const CRYPT_SHA512 = 9;
const CRYPT_STD_DES = 1;
属性¶
/**
* @var int
*/
protected $defaultHash;
/**
* @var int
*/
protected $numberBytes = 16;
/**
* @var Random
*/
protected $random;
/**
* @var string|null
*/
protected $requestToken;
/**
* @var string|null
*/
protected $token;
/**
* @var string|null
*/
protected $tokenKey;
/**
* @var string
*/
protected $tokenKeySessionId = $PHALCON/CSRF/KEY$;
/**
* @var string
*/
protected $tokenValueSessionId = $PHALCON/CSRF$;
/**
* @var int
*/
protected $workFactor = 10;
/**
* @var SessionInterface|null
*/
private $localSession;
/**
* @var RequestInterface|null
*/
private $localRequest;
方法¶
Security 构造函数。public function checkHash( string $password, string $passwordHash, int $maxPassLength = int ): bool;
public function checkToken( string $tokenKey = null, mixed $tokenValue = null, bool $destroyIfValid = bool ): bool;
加密\Security\异常¶
-
命名空间
Phalcon\Encryption\Security
-
使用
-
继承
\Exception
-
实现
安全组件中抛出的任何异常类型均为
Phalcon\Security 中抛出的异常使用此类
加密\Security\JWT\Builder¶
-
命名空间
Phalcon\Encryption\Security\JWT
-
使用
Phalcon\Encryption\Security\JWT\Exceptions\ValidatorException
Phalcon\Encryption\Security\JWT\Signer\SignerInterface
Phalcon\Encryption\Security\JWT\Token\Enum
Phalcon\Encryption\Security\JWT\Token\Item
Phalcon\Encryption\Security\JWT\Token\Signature
Phalcon\Encryption\Security\JWT\Token\Token
Phalcon\Support\Collection
Phalcon\Support\Collection\CollectionInterface
Phalcon\Support\Helper\Json\Encode
-
继承
-
实现
Builder
该构建器提供了
@property CollectionInterface $claims @property CollectionInterface $jose @property string $passphrase @property SignerInterface $signer
@link https://tools.ietf.org/html/rfc7519
属性¶
/**
* @var CollectionInterface
*/
private $claims;
/**
* @var Encode
*/
private $encode;
/**
* @var CollectionInterface
*/
private $jose;
/**
* @var string
*/
private $passphrase;
/**
* @var SignerInterface
*/
private $signer;
方法¶
构造函数 Builder 添加自定义声明 添加自定义声明 "aud"(受众)声明标识了 JWT 所针对的接收者。任何打算处理该 JWT 的主体都必须使用 audience 声明中的一个值来标识自己。如果在存在此声明的情况下,处理该声明的主体没有在 "aud" 声明中用一个值标识自己,则必须拒绝该 JWT。一般情况下,"aud" 值是一个大小写敏感字符串的数组,每个字符串包含一个 StringOrURI 值。特殊情况下,当 JWT 只有一个受众时,"aud" 值可以是一个单独的大小写敏感字符串,其中包含一个 StringOrURI 值。受众值的具体含义通常由应用程序决定。此声明的使用是可选的。 设置内容类型头部 'cty' "exp"(过期时间)声明标识了一个时间,在此时间之后 JWT 必须不再被接受处理。处理 "exp" 声明时要求当前日期/时间必须早于 "exp" 声明中列出的过期日期/时间。实现者可能允许一些很小的时间宽容度,通常不超过几分钟,以弥补时钟偏差。其值必须是一个包含 NumericDate 值的数字。此声明的使用是可选的。 "jti"(JWT ID)声明为 JWT 提供了唯一的标识符。分配标识符值的方式必须确保相同值意外分配给其他数据对象的可能性可以忽略不计;如果应用程序使用多个签发者,还必须防止不同签发者生成的值之间发生冲突。"jti" 声明可用于防止 JWT 被重播。"jti" 值是一个大小写敏感的字符串。此声明的使用是可选的。 "iat"(签发时间)声明标识了 JWT 签发的时间。此声明可用于确定 JWT 的年龄。其值必须是一个包含 NumericDate 值的数字。此声明的使用是可选的。 "iss"(签发者)声明标识了签发 JWT 的主体。对此声明的处理通常取决于具体的应用程序。"iss" 值是一个包含 StringOrURI 值的大小写敏感字符串。此声明的使用是可选的。 "nbf"(生效时间)声明标识了一个时间,在此时间之前 JWT 必须不能被接受处理。处理 "nbf" 声明时要求当前日期/时间必须晚于或等于 "nbf" 声明中列出的生效日期/时间。实现者可能允许一些很小的时间宽容度,通常不超过几分钟,以弥补时钟偏差。其值必须是一个包含 NumericDate 值的数字。此声明的使用是可选的。 "sub"(主题)声明标识了 JWT 的主题。通常来讲,JWT 中的声明都是关于这个主题的陈述。subject 值要么在其签发者的上下文中具有局部唯一性,要么全局唯一。对此声明的处理通常取决于具体的应用程序。"sub" 值是一个包含 StringOrURI 值的大小写敏感字符串。此声明的使用是可选的。 设置已注册声明Encryption\Security\JWT\Exceptions\UnsupportedAlgorithmException¶
-
命名空间
Phalcon\Encryption\Security\JWT\Exceptions
-
使用
Exception
-
继承
Exception
-
实现
当算法不被 JWT 支持时抛出的异常
Encryption\Security\JWT\Exceptions\ValidatorException¶
-
命名空间
Phalcon\Encryption\Security\JWT\Exceptions
-
使用
Exception
-
继承
Exception
-
实现
当 JWT 验证未通过时抛出的异常
Encryption\Security\JWT\Signer\AbstractSigner
¶
-
命名空间
Phalcon\Encryption\Security\JWT\Signer
-
使用
-
继承
-
实现
SignerInterface
抽象类,用于辅助签名类
属性¶
方法¶
Encryption\Security\JWT\Signer\Hmac¶
-
命名空间
Phalcon\Encryption\Security\JWT\Signer
-
使用
Phalcon\Encryption\Security\JWT\Exceptions\UnsupportedAlgorithmException
-
继承
AbstractSigner
-
实现
HMAC 签名类
方法¶
Hmac 构造函数 返回用于 "alg" 头部的值 使用密码短语对数据载荷进行签名 使用给定的数据源、数据载荷和密码短语进行验证Encryption\Security\JWT\Signer\None¶
-
命名空间
Phalcon\Encryption\Security\JWT\Signer
-
使用
-
继承
-
实现
SignerInterface
无签名类
方法¶
返回用于 "alg" 头部的值 返回使用的算法 使用密码短语对数据载荷进行签名 使用给定的数据源、数据载荷和密码短语进行验证Encryption\Security\JWT\Signer\SignerInterface
¶
-
命名空间
Phalcon\Encryption\Security\JWT\Signer
-
使用
-
继承
-
实现
JWT 签名类接口
方法¶
返回用于 "alg" 头部的值 返回使用的算法 使用密码短语对数据载荷进行签名 使用给定的数据源、数据载荷和密码短语进行验证Encryption\Security\JWT\Token\AbstractItem
¶
-
命名空间
Phalcon\Encryption\Security\JWT\Token
-
使用
-
继承
-
实现
token 类的抽象辅助类
属性¶
方法¶
Encryption\Security\JWT\Token\Enum¶
-
命名空间
Phalcon\Encryption\Security\JWT\Token
-
使用
-
继承
-
实现
token 常量。提供 Headers 和 Claims 的常量
@link https://tools.ietf.org/html/rfc7519
常量¶
const ALGO = alg;
const AUDIENCE = aud;
const CONTENT_TYPE = cty;
const EXPIRATION_TIME = exp;
const ID = jti;
const ISSUED_AT = iat;
const ISSUER = iss;
const NOT_BEFORE = nbf;
const SUBJECT = sub;
const TYPE = typ;
Encryption\Security\JWT\Token\Item¶
-
命名空间
Phalcon\Encryption\Security\JWT\Token
-
使用
-
继承
AbstractItem
-
实现
token 条目的存储类
方法¶
Item构造函数。Encryption\Security\JWT\Token\Parser¶
-
命名空间
Phalcon\Encryption\Security\JWT\Token
-
使用
InvalidArgumentException
Phalcon\Support\Helper\Json\Decode
-
继承
-
实现
token 解析类。
它通过验证 token 是否格式正确并将其拆分为三个部分来进行解析。首先解码头部,然后是声明,最后是签名。它返回一个带有解码信息填充的 token 对象。
属性¶
方法¶
解析 token 并返回它Encryption\Security\JWT\Token\Signature¶
-
命名空间
Phalcon\Encryption\Security\JWT\Token
-
使用
-
继承
AbstractItem
-
实现
包含编码数据和哈希的签名类
方法¶
Signature 构造函数Encryption\Security\JWT\Token\Token¶
-
命名空间
Phalcon\Encryption\Security\JWT\Token
-
使用
Phalcon\Encryption\Security\JWT\Signer\SignerInterface
Phalcon\Encryption\Security\JWT\Validator
-
继承
-
实现
令牌类。
用于存储与令牌相关的数据的容器。它存储声明(claims)、头部(headers)、签名(signature)和负载(payload)。同时计算并返回令牌字符串。
@link https://tools.ietf.org/html/rfc7519
属性¶
/**
* @var Item
*/
private $claims;
/**
* @var Item
*/
private $headers;
/**
* @var Signature
*/
private $signature;
方法¶
令牌构造函数。 返回已注册的声明。 返回已注册的头部信息。 返回负载内容。 返回签名 返回令牌。 验证签名。加密\安全\JWT\验证器¶
-
命名空间
Phalcon\Encryption\Security\JWT
-
使用
Phalcon\Encryption\Security\JWT\Exceptions\ValidatorException
Phalcon\Encryption\Security\JWT\Signer\SignerInterface
Phalcon\Encryption\Security\JWT\Token\Enum
Phalcon\Encryption\Security\JWT\Token\Token
-
继承
-
实现
验证器类。
属性¶
/**
* @var array
*/
private $claims;
/**
* @var array
*/
private $errors;
/**
* @var int
*/
private $timeShift = ;
/**
* @var Token
*/
private $token;
方法¶
验证器构造函数。 返回声明的值。 返回包含验证错误的数组(如果有的话)。 设置声明的值,以便与令牌中的值进行比较。 设置要验证的令牌。 验证受众(audience)。 验证令牌的过期时间。 验证令牌的ID。 验证令牌的签发时间(iat)。 验证令牌的签发者(issuer)。 验证令牌的生效时间(nbf)。 验证令牌的签名。加密\安全\随机数生成器¶
-
命名空间
Phalcon\Encryption\Security
-
使用
-
继承
-
实现
Phalcon\Encryption\Security\Random
安全随机数生成器类。
提供适合用于生成HTTP Cookie中的会话密钥等的安全随机数生成器。
Phalcon\Encryption\Security\Random
可主要用于:
- 密钥生成(例如复杂密钥的生成)
- 为新用户账户生成随机密码
- 加密系统
$random = new \Phalcon\Encryption\Security\Random();
// Random binary string
$bytes = $random->bytes();
// Random hex string
echo $random->hex(10); // a29f470508d5ccb8e289
echo $random->hex(10); // 533c2f08d5eee750e64a
echo $random->hex(11); // f362ef96cb9ffef150c9cd
echo $random->hex(12); // 95469d667475125208be45c4
echo $random->hex(13); // 05475e8af4a34f8f743ab48761
// Random base62 string
echo $random->base62(); // z0RkwHfh8ErDM1xw
// Random base64 string
echo $random->base64(12); // XfIN81jGGuKkcE1E
echo $random->base64(12); // 3rcq39QzGK9fUqh8
echo $random->base64(); // DRcfbngL/iOo9hGGvy1TcQ==
echo $random->base64(16); // SvdhPcIHDZFad838Bb0Swg==
// Random URL-safe base64 string
echo $random->base64Safe(); // PcV6jGbJ6vfVw7hfKIFDGA
echo $random->base64Safe(); // GD8JojhzSTrqX7Q8J6uug
echo $random->base64Safe(8); // mGyy0evy3ok
echo $random->base64Safe(null, true); // DRrAgOFkS4rvRiVHFefcQ==
// Random UUID
echo $random->uuid(); // db082997-2572-4e2c-a046-5eefe97b1235
echo $random->uuid(); // da2aa0e2-b4d0-4e3c-99f5-f5ef62c57fe2
echo $random->uuid(); // 75e6b628-c562-4117-bb76-61c4153455a9
echo $random->uuid(); // dc446df1-0848-4d05-b501-4af3c220c13d
// Random number between 0 and $len
echo $random->number(256); // 84
echo $random->number(256); // 79
echo $random->number(100); // 29
echo $random->number(300); // 40
// Random base58 string
echo $random->base58(); // 4kUgL2pdQMSCQtjE
echo $random->base58(); // Umjxqf7ZPwh765yR
echo $random->base58(24); // qoXcgmw4A9dys26HaNEdCRj9
echo $random->base58(7); // 774SJD3vgP
该类部分借鉴了 Ruby 的 SecureRandom 库
@link https://ruby-doc.org/stdlib-2.2.2/libdoc/securerandom/rdoc/SecureRandom.html
方法¶
生成一个随机的 base58 字符串。如果未指定 $len,则默认为 16。将来可能会更大。结果可能包含字母数字字符,但不包括 0、O、I 和 l。
它类似于Phalcon\Encryption\Security\Random::base64()
但已修改以避免非字母数字字符和打印时可能看起来模糊的字母。
@see \Phalcon\Encryption\Security\Random::base64 @link https://en.wikipedia.org/wiki/Base58 @throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
生成一个随机的 base62 字符串。如果未指定 $len,则默认为 16。将来可能会更大。
它类似于Phalcon\Encryption\Security\Random::base58()
但进行了修改以提供可用于URL的最大值且无需考虑额外字符因为它使用的是[A-Za-z0-9]。
@see \Phalcon\Encryption\Security\Random::base58 @throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
生成一个随机的 base64 字符串。如果未指定 $len,则默认为 16。将来可能会更大。结果字符串的长度通常大于 $len。大小公式:4($len / 3) 向上舍入到4的倍数。
@throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
生成一个随机的 URL 安全 base64 字符串。如果未指定 $len,则默认为 16。将来可能会更大。结果字符串的长度通常大于 $len。
默认情况下不生成填充字符,因为 "=" 可能会被用作 URL 分隔符。结果可以包含 A-Z、a-z、0-9、"-" 和 "_"。如果 $padding 为 true 则会包含 "="。参见 RFC 3548 中关于 URL 安全 base64 的定义。
$random = new \Phalcon\Encryption\Security\Random();
echo $random->base64Safe(); // GD8JojhzSTrqX7Q8J6uug
@link https://www.ietf.org/rfc/rfc3548.txt @throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
生成一个随机的二进制字符串。The Random::bytes
方法返回一个字符串,并接受表示所需返回字节长度的整数作为输入。
如果未指定 $len,则默认为 16。将来可能会更大。结果可能包含任何字节:"x00" - "xFF"。
$random = new \Phalcon\Encryption\Security\Random();
$bytes = $random->bytes();
var_dump(bin2hex($bytes));
// Possible output: string(32) "00f6c04b144b41fad6a59111c126e1ee"
@throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
生成一个随机的十六进制字符串。如果未指定 $len,则默认为 16。将来可能会更大。结果字符串的长度通常大于 $len。
@throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
生成一个介于 0 和 $len 之间的随机数。返回一个整数:0 <= 结果 <= $len。
@throws 异常 如果安全随机数生成器不可用、发生意外部分读取或者 $len <= 0 生成一个 v4 版本的随机 UUID(通用唯一标识符)。版本 4 的 UUID 是完全随机的(除了版本号)。它不包含有意义的信息如 MAC 地址、时间等。详情请参阅 RFC 4122。
此算法设置了版本号(4位)以及两个保留位。所有其他位(剩余的122位)使用随机或伪随机数据源设置。版本 4 的 UUID 格式为 xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,其中 x 是任意十六进制数字,y 是 8、9、A 或 B(例如:f47ac10b-58cc-4372-a567-0e02b2c3d479)。
$random = new \Phalcon\Encryption\Security\Random();
echo $random->uuid(); // 1378c906-64bb-4f81-a8d6-4ae1bfcdec22
@link https://www.ietf.org/rfc/rfc4122.txt @throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取
基于字符数 ($base) 和字符集 ($alphabet) 生成一个随机字符串。如果未指定 $n,则默认为 16。将来可能会更大。
@throws 异常 如果安全随机数生成器不可用或发生了意外的部分读取