您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關laravel 5.3中怎么自定義加密服務,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
創建加密解密服務類
文件地址 /app/Service/Common/CryptService.php 代碼如下
下面這個是個人寫的支持長字符串的RSA加密類作為示例,自定義加密的話只需更改這個文件的代碼就好,其它操作只是為了實現依賴注入。
<?php namespace App\Service\Common; class CryptService { public $config,$keypath, $prikey_path, $pubkey_path, $prikey, $pubkey , $private_key_size; public function select($select = 'rsa_api') { $config = config('crypt'); if (array_key_exists($select, $config)) { $this->config = $config[$select]; $this->private_key_size = $this->config['openssl_config']['private_key_bits']; } else { return false; } $this->keypath = dirname(dirname(dirname(__DIR__))) . $this->config['path']; if(!file_exists($this->keypath)){ mkdir($this->keypath,"0777",true); } $this->prikey_path = $this->keypath . $this->config['private_key_file_name']; $this->pubkey_path = $this->keypath . $this->config['public_key_file_name']; if (file_exists($this->prikey_path)) $this->prikey = file_get_contents($this->prikey_path); if (file_exists($this->pubkey_path)) $this->pubkey = file_get_contents($this->pubkey_path); return $this; } public function makeKey() { $res = openssl_pkey_new($this->config['openssl_config']); openssl_pkey_export($res, $this->prikey); file_put_contents($this->prikey_path, $this->prikey); $pubkey = openssl_pkey_get_details($res); $this->pubkey = $pubkey['key']; file_put_contents($this->pubkey_path, $this->pubkey); return $test = ['prikey' => $this->prikey, 'pubkey' => $this->pubkey]; } public function encryptPrivate($data){ $crypt = $this->encrypt_split($data); $crypted = ''; foreach ($crypt as $k=>$c){ if($k!=0) $crypted.="@"; $crypted.=base64_encode($this->doEncryptPrivate($c)); } return $crypted; } public function encryptPublic($data){ $crypt = $this->encrypt_split($data); $crypted = ''; foreach ($crypt as $k=>$c){ if($k!=0) $crypted.="@"; $crypted.=base64_encode($this->doEncryptPublic($c)); } return $crypted; } public function decryptPublic($data){ $decrypt = explode('@',$data); $decrypted = ""; foreach ($decrypt as $k=>$d){ $decrypted .= $this->doDecryptPublic(base64_decode($d)); } return $decrypted; } public function decryptPrivate($data){ $decrypt = explode('@',$data); $decrypted = ""; foreach ($decrypt as $k=>$d){ $decrypted .= $this->doDecryptPrivate(base64_decode($d)); } return $decrypted; } private function encrypt_split($data){ $crypt=[];$index=0; for($i=0; $i<strlen($data); $i+=117){ $src = substr($data, $i, 117); $crypt[$index] = $src; $index++; } return $crypt; } private function doEncryptPrivate($data) { $rs = ''; if (@openssl_private_encrypt($data, $rs, $this->prikey) === FALSE) { return NULL; } return $rs; } private function doDecryptPrivate($data) { $rs = ''; if (@openssl_private_decrypt($data, $rs, $this->prikey) === FALSE) { return null; } return $rs; } private function doEncryptPublic($data){ $rs = ''; if (@openssl_public_encrypt($data, $rs, $this->pubkey) === FALSE) { return NULL; } return $rs; } private function doDecryptPublic($data) { $rs = ''; if (@openssl_public_decrypt($data, $rs, $this->pubkey) === FALSE) { return null; } return $rs; } }
創建門面facades
文件地址 /app/Facades/CryptFacades.php 代碼如下:
<?php namespace App\Facades; use \Illuminate\Support\Facades\Facade; class CryptFacades extends Facade{ public static function getFacadeAccessor() { return 'MyCrypt'; } }
注冊服務
創建文件 /app/Providers/MyCryptServiceProvider.php 代碼如下:
其實也可以在AppServiceProvider中注冊,就不用另外建個MyCryptServiceProvider.php文件了
而且在/config/app.php中一般也已經有了AppServiceProvider的聲明
<?php namespace App\Providers; use App\Service\Common\CryptService; use Illuminate\Support\ServiceProvider; class MyCryptServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { \App::bind('MyCrypt',CryptService::class); } }
在配置中聲明
文件地址 /config/app.php 在providershe和aliases中添加
'providers' => [ \App\Providers\MyCryptServiceProvider::class, ], 'aliases' => [ 'MyCrypt' => \App\Facades\CryptFacades::class, ]
編寫自定義加密解密服務的配置文件
/config/crypt.php 因為我寫的CryptService有用到配置文件,所以需要再添加個配置文件。在實際項目中,可以根據需要自行設置配置文件和加密服務類。
<?php //基于laravel根目錄,分隔符最好是用 DIRECTORY_SEPARATOR 常量代替 return [ 'rsa_api' => [ 'path'=>DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'rsakey'.DIRECTORY_SEPARATOR, 'private_key_file_name'=>'private_key.pem', 'public_key_file_name' =>'public_key.pem', 'openssl_config'=>[ "digest_alg" => "sha512", "private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA, ] ], 'rsa_data'=>[ 'path'=>DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'rsakey'.DIRECTORY_SEPARATOR, 'private_key_file_name'=>'private.pem', 'public_key_file_name' =>'public.pem', 'openssl_config'=>[ "digest_alg" => "sha512", "private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA, ] ] ];
在Controller中使用的示例
1、artisan創建Controller文件
php artisan make:controller IndexController
2、編輯IndexController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use MyCrypt; class IndexController extends Controller{ public function test(){ $crypt = MyCrypt::select('rsa_api'); $crypt->makeKey(); $short = "abcd"; $long = " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; $req['short'] = $short; $req['short_private_encrypt'] = $crypt->encryptPrivate($short); $req['short_public_decrypt'] = $crypt->decryptPublic($req['short_private_encrypt']); $req['long'] = $long; $req['long_private_encrypt'] = $crypt->encryptPrivate($long); $req['long_public_decrypt'] = $crypt->decryptPublic($req['long_private_encrypt']); dump($req); //dd($req); } }
3、在/routes/web.php添加路由
Route::get('/test', 'IndexController@test');
看完上述內容,你們對laravel 5.3中怎么自定義加密服務有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。