使用php的openssl擴充套件生成RSA證照,公鑰和私鑰金鑰對
<?php
class OpensslServer
{
protected $expire;
protected $pass_phrase;
protected $path;
protected $private_key;
protected $time;
public $dn = [
"countryName" => "CN", "stateOrProvinceName" => "ChongQing", "localityName" => "China",
"organizationName" => "The Brain Room Limited", "organizationalUnitName" => "PHP Documentation Team",
"commonName" => "Rancy Bruce", "emailAddress" => "rancy@rancy.top"
];
public $config = [
"private_key_bits" => 1024,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
];
public function __construct($path = null, $expire = 365, $pass_phrase = 'rancy')
{
$this->path = $path;
$this->expire = $expire;
$this->pass_phrase = $pass_phrase;
$this->time = time();
}
public function generate()
{
$res = openssl_pkey_new($this->config);
openssl_pkey_export($res, $this->private_key, $this->pass_phrase, $this->config);
$pubKey = openssl_pkey_get_details($res);
file_put_contents("{$this->path}/{$this->time}_private.key", $this->private_key);
file_put_contents("{$this->path}/{$this->time}_public.key", $pubKey["key"]);
}
public function cert()
{
$csr = openssl_csr_new($this->dn, $this->private_key, $this->config);
$csr_sign = openssl_csr_sign($csr, null, $this->private_key, $this->expire, $this->config);
openssl_x509_export($csr_sign, $csr_key);
openssl_pkcs12_export($csr_sign, $private_pkcs12, $this->private_key, $this->pass_phrase);
file_put_contents("{$this->path}/{$this->time}_cert.cer", $csr_key);
file_put_contents("{$this->path}/{$this->time}_private.pfx", $private_pkcs12);
}
}
使用方法
<?php
$openssl = new OpensslServer('certs');
$openssl->generate();
$openssl->cert();
本作品採用《CC 協議》,轉載必須註明作者和本文連結