12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Services;
- use App\Traits\CommonTrait;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- class OpensslService
- {
- use CommonTrait;
- public $table = 'sys_openssl_secret';
- /**
- * 重新缓存数据
- */
- public function cacheCsr()
- {
- $query = DB::table($this->table);
- $query->select('id')->orderBy('id', 'asc')->chunk(100, function ($listObj) {
- foreach ($listObj as $row) {
- $keyArr = $this->getOpensslCacheKey($row->id);
- Cache::tags($keyArr['tags'])->forever($keyArr['key'], get_object_vars($row));
- }
- });
- }
- /**
- * 创建新的密钥对
- *
- * @return array
- */
- public function getNewCsr()
- {
- $config = array(
- "private_key_bits" => 1024, //指定应该使用多少位来生成私钥 512 1024 2048 4096等
- "private_key_type" => OPENSSL_KEYTYPE_RSA, //选择在创建CSR时应该使用哪些扩展。可选值有 OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA 或 OPENSSL_KEYTYPE_EC. 默认值是 OPENSSL_KEYTYPE_RSA.
- );
- $res = openssl_pkey_new($config);
- openssl_pkey_export($res, $private_key_pem, null, $config);
- $details = openssl_pkey_get_details($res);
- $public_key_pem = $details['key'];
- return ['private_key' => $private_key_pem, 'public_key' => $public_key_pem];
- }
- /**
- * 清空表,清空缓存
- */
- public function clearTable()
- {
- DB::table($this->table)->truncate();
- $keyArr = $this->getOpensslTotalCacheKey();
- Cache::tags($keyArr['tags'])->flush();
- }
- }
|