php sleep usleep php usleep php使用
# 如何在回调函数中访问外部的变量在swoole _服务器/swoole _客户端的事件回调函数中,需要读取调用外部的变量和对象,可以通过下面的几种方法实现。* * * * *[TOC=2,3]*
2021-10-21 14:37:13
458
在PHP(PHP 5 >= 5.3.0, PHP 7, PHP 8)中,openssl_encrypt的参数大致如下:
openssl_encrypt(
string $data,
string $cipher_algo,
string $passphrase,
int $options = 0,
string $iv = "",
string &$tag = null,
string $aad = "",
int $tag_length = 16
): string|false
其中$data
待加密的数据字符串,这很好理解,这里不做过多阐释。
$cipher_algo
为加密算法,如aes-128-ecb
,中间这个数字表示密钥长度为128位。所有加密算法可通过openssl_get_cipher_methods获得。$passphrase
为密钥。既然上面的算法都规定了密钥长度,那这个密钥的字符串长度就已经确定下来了。但PHP随性的一点是如果密钥小于或超过算法指定的长度,也都能正常返回加密结果,而不像Java等语言直接抛出密钥长度不正确的异常。根据手册解释:
若 passphrase 比预期长度短,将静默用 NUL 填充; 若比预期长度更长,将静默截断。
那么NUL在PHP中表现又是什么?查了PHP源代码下的openssl实现,找到如下代码:
if (key_len > password_len) {
if ((OPENSSL_DONT_ZERO_PAD_KEY & options) && !EVP_CIPHER_CTX_set_key_length(cipher_ctx, password_len)) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Key length cannot be set for the cipher algorithm");
return FAILURE;
}
key = emalloc(key_len);
memset(key, 0, key_len);
memcpy(key, *ppassword, password_len);
*ppassword = (char *) key;
*ppassword_len = key_len;
*free_password = 1;
}
memset(key, 0, key_len); 应该对应的就是ASCII中的零,也就是PHP里的chr(0)。所以如果一个密钥长度不够,PHP会自动在密钥后面用chr(0)不足。例如:aes-128-ecb
算法,密钥长度应为128位,也就是字符串长度为16。这时如果密钥为"1234567890abcde"和"1234567890abcde".chr(0)的意义是一样的,得到的加密结果也是一样的。至于超出长度自动截断,这应该很好理解了。
$options
是以下标记的按位或: OPENSSL_RAW_DATA 、 OPENSSL_ZERO_PADDING, 指定对于数据,如何填充的。OPENSSL_RAW_DATA
的含义应该是不经过base64_encode,即raw data。如果没有这个掩码,则会返回base64字符串。 30
2022-11
30
2022-11
30
2022-11
17
2022-11
17
2022-11
31
2022-10