How do I customize Cryptographic services in Laravel? This article mainly introduces the Laravel 5.3 in the custom encryption service related data, the article introduced in very detailed, to everybody study or uses Laravel 5.3 has certain reference learning value, needs the friend below to see together. We hope to help you.
Objective
This article describes a scheme for customizing cryptographic Services in Laravel 5.3, using Laravel's service container to implement custom cryptographic service registrations (examples are RSA encryption that supports long strings), and here's a look at the detailed description:
Creating the cryptographic Decryption service class
The file address/app/service/common/cryptservice.php code is as follows
The following is a personal write to support long string RSA encryption class as an example, custom encryption only need to change the code of this file is good, other operations just to implement dependency injection.
<?phpnamespace 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 ' and $this->pubkey]; The 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; The 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; The Public Function decryptpublic ($data) {$decrypt = explode (' @ ', $data); $decrypted = ""; foreach ($decrypt as $k =>$d) {$decrypted. = $this->dodecryptpublic (Base64_decode ($d)); } return $decrypted; The 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; }}
Create a façade facades
The file address/app/facades/cryptfacades.php code is as follows:
<?phpnamespace app\facades;use \illuminate\support\facades\facade;class Cryptfacades extends Facade{public static function Getfacadeaccessor () { return ' Mycrypt ';}}
Registration Services
Create the file/app/providers/mycryptserviceprovider.php code as follows:
In fact, you can also register in Appserviceprovider, you don't have to build another mycryptserviceprovider.php file.
And in the/config/app.php, there's a appserviceprovider statement in general.
<?phpnamespace 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);}}
Declared in configuration
File address/config/app.php added in Providershe and aliases
' Providers ' = [\app\providers\mycryptserviceprovider::class,], ' aliases ' = [' mycrypt ' and ' = ' \app\facades\ Cryptfacades::class,]
Writing a configuration file for a custom cryptographic decryption service
/config/crypt.php because I wrote the Cryptservice useful to the configuration file, so I need to add a configuration file. In a real-world project, you can set your own profile and cryptographic service classes as needed.
<?php//based on the Laravel root directory, the delimiter is best replaced with a Directory_separator constant instead of 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 ' and ' sha512 ', ' private_key_bits ' and ', ' 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" and "sha512", "private_key_bits" and "1024", "Private_key_type" = Openssl_keytype_rsa, ] ]];
Examples used in a controller
1. Artisan Create Controller file
PHP Artisan Make:controller Indexcontroller
2. Edit Indexcontroller
<?phpnamespace 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. Adding routes in/routes/web.php
Route::get ('/test ', ' indexcontroller@test ');
4. Browser Access Validation Results
Related recommendations:
Explore how the middleware of Laravel is implemented
Laravel writing the App interface (API)
Some practical tips for improving the performance of Laravel 5