PHP code example of codelieutenant / laravel-crypto

1. Go to this page and download the library: Download codelieutenant/laravel-crypto library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codelieutenant / laravel-crypto example snippets


return [
    App\Providers\AppServiceProvider::class,
    // Illuminate\Encryption\EncryptionServiceProvider::class, // Remove or comment out
    CodeLieutenant\LaravelCrypto\ServiceProvider::class,       // Add this
];

'providers' => [
    // ...
    // Illuminate\Encryption\EncryptionServiceProvider::class,
    CodeLieutenant\LaravelCrypto\ServiceProvider::class,
    // ...
],

'cipher' => 'Sodium_AES256GCM', // Options: Sodium_AES256GCM, Sodium_XChaCha20Poly1305, Sodium_AEGIS256GCM, Sodium_AEGIS128LGCM, Sodium_SecretBox

use Illuminate\Support\Facades\Crypt;

$encrypted = Crypt::encryptString('Hello Sodium');
$decrypted = Crypt::decryptString($encrypted);

use Illuminate\Support\Facades\Crypt;

// Encrypt a file
Crypt::encryptFile('path/to/input.txt', 'path/to/output.enc');

// Decrypt a file
Crypt::decryptFile('path/to/output.enc', 'path/to/decrypted.txt');

use CodeLieutenant\LaravelCrypto\Facades\Hashing;

$hash = Hashing::hash('data');

use CodeLieutenant\LaravelCrypto\Facades\Sign;

// HMAC
$sig = Sign::sign('message');

// EdDSA
$sig = Sign::eddsaSign('message');

use CodeLieutenant\LaravelCrypto\Casts\EncryptedFileCast;
use Illuminate\Database\Eloquent\Model;

class Document extends Model
{
    protected $casts = [
        'file' => EncryptedFileCast::class,
    ];
}

// Accessing the file decrypts it to a temporary location
$content = $document->file->contents();

// Modifying the content and saving the model re-encrypts the file
$document->file->putContents('Secret Data');
$document->save();

use CodeLieutenant\LaravelCrypto\Casts\UserEncryptedWithIndex;
use Illuminate\Database\Eloquent\Model;

class UserSecret extends Model
{
    protected function casts(): array
    {
        return [
            'ssn' => UserEncryptedWithIndex::class . ':ssn_index',
        ];
    }
}
bash
php artisan vendor:publish --provider="CodeLieutenant\LaravelCrypto\ServiceProvider"
bash
php artisan crypto:keys