OpenSSL 函数 业 ,精于勤 荒于嬉.
- OpenSSL 函数 openssl_cipher_iv_length 获取密码iv长度
-
发表日期:2021-07-01 08:55:20 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php $method = 'AES-128-CBC'; $ivlen = openssl_cipher_iv_length($method); echo $ivlen; ?>
- OpenSSL 函数 openssl_cms_decrypt Decrypt a CMS message
-
发表日期:2021-07-01 08:55:20 | 来源: | 分类:OpenSSL 函数
-
openssl_cms_decrypt
(PHP 8)
openssl_cms_decrypt — Decrypt a CMS message
说明
openssl_cms_decrypt(
string$input_filename
,
string$output_filename
,
OpenSSLCertificate|string$certificate
,
OpenSSLAsymmetricKey|OpenSSLCertificate|array|string|null$private_key
=null
,
int$encoding
=OPENSSL_ENCODING_SMIME
): boolDecrypts a CMS message.
参数
-
input_filename
-
The name of a file containing encrypted content.
-
output_filename
-
The name of the file to deposit the decrypted content.
-
certificate
-
The name of the file containing a certificate of the recipient.
-
private_key
-
The name of the file containing a PKCS#8 key.
-
encoding
-
The encoding of the input file. One of
OPENSSL_CMS_SMIME
,OPENSLL_CMS_DER
orOPENSSL_CMS_PEM
.
返回值
成功时返回
true
, 或者在失败时返回false
。 -
- OpenSSL 函数 openssl_cms_encrypt Encrypt a CMS message
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_cms_encrypt
(PHP 8)
openssl_cms_encrypt — Encrypt a CMS message
说明
openssl_cms_encrypt(
string$input_filename
,
string$output_filename
,
OpenSSLCertificate|array|string$certificate
,
array|null$headers
,
int$flags
= 0,
int$encoding
=OPENSSL_ENCODING_SMIME
,
int$cipher_algo
=OPENSSL_CIPHER_RC2_40
): boolThis function encrypts content to one or more recipients, based on the certificates that are passed to it.
参数
-
input_filename
-
The file to be encrypted.
-
output_filename
-
The output file.
-
certificate
-
Recipients to encrypt to.
-
headers
-
Headers to include when S/MIME is used.
-
flags
-
Flags to be passed to CMS_sign.
-
encoding
-
An encoding to output. One of
OPENSSL_CMS_SMIME
,OPENSLL_CMS_DER
orOPENSSL_CMS_PEM
. -
cipher_algo
-
A cypher to use.
返回值
成功时返回
true
, 或者在失败时返回false
。 -
- OpenSSL 函数 openssl_cms_read Export the CMS file to an array of PEM certificates
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_cms_read
(PHP 8)
openssl_cms_read — Export the CMS file to an array of PEM certificates
说明
openssl_cms_read(string$input_filename
, array&$certificates
): boolPerforms the exact analog to openssl_pkcs7_read().
警告本函数还未编写文档,仅有参数列表。
参数
-
input_filename
-
-
certificates
-
返回值
成功时返回
true
, 或者在失败时返回false
。 -
- OpenSSL 函数 openssl_cms_sign Sign a file
-
发表日期:2021-07-01 08:55:20 | 来源: | 分类:OpenSSL 函数
-
openssl_cms_sign
(PHP 8)
openssl_cms_sign — Sign a file
说明
openssl_cms_sign(
string$input_filename
,
string$output_filename
,
OpenSSLCertificate|string$certificate
,
OpenSSLAsymmetricKey|OpenSSLCertificate|array|string$private_key
,
array|null$headers
,
int$flags
= 0,
int$encoding
=OPENSSL_ENCODING_SMIME
,
string|null$untrusted_certificates_filename
=null
): boolThis function signs a file with an X.509 certificate and key.
参数
-
input_filename
-
The name of the file to be signed.
-
output_filename
-
The name of the file to deposit the results.
-
certificate
-
The name of the file containing the signing certificate.
-
private_key
-
The name of file containing the key associated with
certificate
. -
headers
-
An array of headers to be included in S/MIME output.
-
flags
-
Flags to be passed to cms_sign().
-
encoding
-
The encoding of the output file. One of
OPENSSL_CMS_SMIME
,OPENSLL_CMS_DER
orOPENSSL_CMS_PEM
. -
untrusted_certificates_filename
-
Intermediate certificates to be included in the signature.
返回值
成功时返回
true
, 或者在失败时返回false
。 -
- OpenSSL 函数 openssl_cms_verify Verify a CMS signature
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_cms_verify
(PHP 8)
openssl_cms_verify — Verify a CMS signature
说明
openssl_cms_verify(
string$input_filename
,
int$flags
= 0,
string|null$certificates
=null
,
array$ca_info
= [],
string|null$untrusted_certificates_filename
=null
,
string|null$content
=null
,
string|null$pk7
=null
,
string|null$sigfile
=null
,
int$encoding
=OPENSSL_ENCODING_SMIME
): boolThis function verifies a CMS signature, either attached or detached, with the specified encoding.
参数
-
input_filename
-
The input file.
-
flags
-
Flags to pass to cms_verify().
-
certificates
-
A file with the signer certificate and optionally intermediate certificates.
-
ca_info
-
An array containing self-signed certificate authority certificates.
-
untrusted_certificates_filename
-
A file containing additional intermediate certificates.
-
content
-
A file pointing to the content when signatures are detached.
-
pk7
-
-
sigfile
-
A file to save the signature to.
-
encoding
-
The encoding of the input file. One of
OPENSSL_CMS_SMIME
,OPENSLL_CMS_DER
orOPENSSL_CMS_PEM
.
返回值
成功时返回
true
, 或者在失败时返回false
。 -
- OpenSSL 函数 openssl_csr_export_to_file 将CSR导出到文件
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php $subject = array( "commonName" => "example.com",); $private_key = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA,)); $csr = openssl_csr_new($subject, $private_key, array('digest_alg' => 'sha384') ); openssl_pkey_export_to_file($private_key, 'example-priv.key'); // Along with the subject, the CSR contains the public key corresponding to the private keyopenssl_csr_export_to_file($csr, 'example-csr.pem'); ?>
- OpenSSL 函数 openssl_csr_export 将CSR作为字符串导出
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php $subject = array( "commonName" => "example.com",); $private_key = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA,)); $configargs = array( 'digest_alg' => 'sha256WithRSAEncryption'); $csr = openssl_csr_new($subject, $private_key, $configargs); openssl_csr_export($csr, $csr_string); echo $csr_string; ?>
- OpenSSL 函数 openssl_csr_get_public_key 返回CSR的公钥
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php $subject = array( "commonName" => "example.com",); $private_key = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA,)); $csr = openssl_csr_new($subject, $private_key, array('digest_alg' => 'sha256') ); $public_key = openssl_csr_get_public_key($csr); $info = openssl_pkey_get_details($public_key); echo $info['key']; ?>
- OpenSSL 函数 openssl_csr_get_subject 返回CSR的主题
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php $subject = array( "countryName" => "CA", "stateOrProvinceName" => "Alberta", "localityName" => "Calgary", "organizationName" => "XYZ Widgets Inc", "organizationalUnitName" => "PHP Documentation Team", "commonName" => "Wez Furlong", "emailAddress" => "wez@example.com",); $private_key = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA,)); $configargs = array( 'digest_alg' => 'sha512WithRSAEncryption'); $csr = openssl_csr_new($subject, $privkey, $configargs); print_r(openssl_csr_get_subject($csr)); ?>
- OpenSSL 函数 openssl_csr_new 生成一个 CSR
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php // for SSL server certificates the commonName is the domain name to be secured// for S/MIME email certificates the commonName is the owner of the email address// location and identification fields refer to the owner of domain or email subject to be secured$dn = array( "countryName" => "GB", "stateOrProvinceName" => "Somerset", "localityName" => "Glastonbury", "organizationName" => "The Brain Room Limited", "organizationalUnitName" => "PHP Documentation Team", "commonName" => "Wez Furlong", "emailAddress" => "wez@example.com"); // Generate a new private (and public) key pair$privkey = openssl_pkey_new(array( "private_key_bits" => 2048, "private_key_type" => OPENSSL_KEYTYPE_RSA,)); // Generate a certificate signing request$csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256')); // Generate a self-signed cert, valid for 365 days$x509 = openssl_csr_sign($csr, null, $privkey, $days=365, array('digest_alg' => 'sha256')); // Save your private key, CSR and self-signed cert for later useopenssl_csr_export($csr, $csrout) and var_dump($csrout); openssl_x509_export($x509, $certout) and var_dump($certout); openssl_pkey_export($privkey, $pkeyout, "mypassword") and var_dump($pkeyout); // Show any errors that occurred herewhile (($e = openssl_error_string()) !== false) { echo $e . "\n"; } ?>
示例2
<?php $subject = array( "commonName" => "docs.php.net",); // Generate a new private (and public) key pair$private_key = openssl_pkey_new(array( "private_key_type" => OPENSSL_KEYTYPE_EC, "curve_name" => 'prime256v1',)); // Generate a certificate signing request$csr = openssl_csr_new($subject, $private_key, array('digest_alg' => 'sha384')); // Generate self-signed EC cert$x509 = openssl_csr_sign($csr, null, $private_key, $days=365, array('digest_alg' => 'sha384')); openssl_x509_export_to_file($x509, 'ecc-cert.pem'); openssl_pkey_export_to_file($private_key, 'ecc-private.key'); ?>
- OpenSSL 函数 openssl_csr_sign 用另一个证书签署 CSR (或者本身) 并且生成一个证书
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php // Let's assume that this script is set to receive a CSR that has// been pasted into a textarea from another page$csrdata = $_POST["CSR"]; // We will sign the request using our own "certificate authority"// certificate. You can use any certificate to sign another, but// the process is worthless unless the signing certificate is trusted// by the software/users that will deal with the newly signed certificate// We need our CA cert and its private key$cacert = "file://path/to/ca.crt"; $privkey = array("file://path/to/ca.key", "your_ca_key_passphrase"); $usercert = openssl_csr_sign($csrdata, $cacert, $privkey, 365, array('digest_alg'=>'sha256') ); // Now display the generated certificate so that the user can// copy and paste it into their local configuration (such as a file// to hold the certificate for their SSL server)openssl_x509_export($usercert, $certout); echo $certout; // Show any errors that occurred herewhile (($e = openssl_error_string()) !== false) { echo $e . "\n"; } ?>
- OpenSSL 函数 openssl_decrypt 解密数据
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_decrypt
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
openssl_decrypt — 解密数据
说明
openssl_decrypt(
string$data
,
string$method
,
string$key
,
int$options
= 0,
string$iv
= "",
string$tag
= "",
string$aad
= ""
): string采用原始或base64编码的字符串,并使用给定的方法和密钥对其进行解密。
参数
-
data
-
将被解密的密文。
-
method
-
加密算法,使用openssl_get_cipher_methods()函数获取可用的加密算法列表。
-
key
-
密钥。
-
options
-
options
can be one ofOPENSSL_RAW_DATA
,OPENSSL_ZERO_PADDING
. -
iv
-
非空的初始化向量。
-
tag
-
AEAD密码模式中的身份验证标签。 如果是错误的,验证失败,函数返回
false
. -
aad
-
额外的认证数据。
返回值
The decrypted string on success 或者在失败时返回
false
.错误/异常
如果通过
method
参数传递的是一个未知的加密算法,将会抛出一个E_WARNING
等级的错误。如果通过
iv
参数传递的是一个空值,将会抛出一个E_WARNING
等级的错误。更新日志
版本 说明 5.3.3 添加 iv
参数。5.4.0 将 raw_output
更改至options
。7.1.0 添加了 tag
和aad
参数。 -
- OpenSSL 函数 openssl_dh_compute_key 计算远程DH密钥(公钥)和本地DH密钥的共享密钥
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_dh_compute_key
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
openssl_dh_compute_key — 计算远程DH密钥(公钥)和本地DH密钥的共享密钥
说明
openssl_dh_compute_key(string$pub_key
, resource$dh_key
): string警告本函数还未编写文档,仅有参数列表。
参数
-
pub_key
-
公钥
-
dh_key
-
DH 密钥
返回值
成功,返回计算的密钥, 或者在失败时返回
false
. -
- OpenSSL 函数 openssl_digest 计算摘要
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_digest
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
openssl_digest — 计算摘要
说明
openssl_digest(string$data
, string$method
, bool$raw_output
= false): string使用给定的方法计算给定数据的摘要哈希值,并返回一个原始的或16进制编码的字符串。
警告本函数还未编写文档,仅有参数列表。
参数
-
data
-
给定的数据。
-
method
-
要使用的摘要方法,比如 "sha256", 查看 openssl_get_md_methods() 函数获取更多可用的摘要方法。
-
raw_output
-
为
true
时将会返回原始输出数据,否则返回值将会是16进制。
返回值
成功,返回摘要哈希值, 或者在失败时返回
false
.错误/异常
如果通过
method
参数传递的是一个未知的摘要算法,将会抛出一个E_WARNING
级的错误。 -
- OpenSSL 函数 openssl_encrypt 加密数据
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php //$key should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes$plaintext = "message to be encrypted"; $cipher = "aes-128-gcm"; if (in_array($cipher, openssl_get_cipher_methods())){ $ivlen = openssl_cipher_iv_length($cipher); $iv = openssl_random_pseudo_bytes($ivlen); $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag); //store $cipher, $iv, and $tag for decryption later $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag); echo $original_plaintext."\n"; } ?>
示例2
<?php //$key previously generated safely, ie: openssl_random_pseudo_bytes$plaintext = "message to be encrypted"; $ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC"); $iv = openssl_random_pseudo_bytes($ivlen); $ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv); $hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true); $ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw ); //decrypt later....$c = base64_decode($ciphertext); $ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC"); $iv = substr($c, 0, $ivlen); $hmac = substr($c, $ivlen, $sha2len=32); $ciphertext_raw = substr($c, $ivlen+$sha2len); $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv); $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true); if (hash_equals($hmac, $calcmac))//PHP 5.6+ timing attack safe comparison{ echo $original_plaintext."\n"; } ?>
- OpenSSL 函数 openssl_error_string 返回 openSSL 错误消息
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php // lets assume you just called an openssl function that failedwhile ($msg = openssl_error_string()) echo $msg . "<br />\n"; ?>
- OpenSSL 函数 openssl_free_key 释放密钥资源
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
openssl_free_key
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
openssl_free_key — 释放密钥资源
说明
openssl_free_key(resource$key_identifier
): voidopenssl_free_key() 从内存中释放和指定的
key_identifier
相关联的密钥。参数
-
key_identifier
-
返回值
没有返回值。
-
- OpenSSL 函数 openssl_get_cert_locations 检索可用的证书位置
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php var_dump(openssl_get_cert_locations()); ?>
- OpenSSL 函数 openssl_get_cipher_methods 获取可用的加密算法
-
发表日期:2021-07-01 08:55:21 | 来源: | 分类:OpenSSL 函数
-
示例1
<?php $ciphers = openssl_get_cipher_methods(); $ciphers_and_aliases = openssl_get_cipher_methods(true); $cipher_aliases = array_diff($ciphers_and_aliases, $ciphers); //ECB mode should be avoided$ciphers = array_filter( $ciphers, function($n) { return stripos($n,"ecb")===FALSE; } ); //At least as early as Aug 2016, Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based$ciphers = array_filter( $ciphers, function($c) { return stripos($c,"des")===FALSE; } ); $ciphers = array_filter( $ciphers, function($c) { return stripos($c,"rc2")===FALSE; } ); $ciphers = array_filter( $ciphers, function($c) { return stripos($c,"rc4")===FALSE; } ); $ciphers = array_filter( $ciphers, function($c) { return stripos($c,"md5")===FALSE; } ); $cipher_aliases = array_filter($cipher_aliases,function($c) { return stripos($c,"des")===FALSE; } ); $cipher_aliases = array_filter($cipher_aliases,function($c) { return stripos($c,"rc2")===FALSE; } ); print_r($ciphers); print_r($cipher_aliases); ?>
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)