60 lines
1.5 KiB
PHP
60 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class EncryptionHelper
|
|
{
|
|
/**
|
|
* Encrypt the given data.
|
|
*/
|
|
public static function encryptData($data)
|
|
{
|
|
try{
|
|
return Crypt::encryptString(json_encode($data));
|
|
}catch(\Exception $e){
|
|
throw new \RuntimeException("Error al encriptar los datos: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Decrypt the given data.
|
|
*/
|
|
public static function decryptData($encryptedData)
|
|
{
|
|
try{
|
|
$decrypted = Crypt::decryptString($encryptedData);
|
|
return json_decode($decrypted, true);
|
|
}catch(DecryptException $e){
|
|
Log::error('Error al desencriptar los datos: ' . $e->getMessage());
|
|
return null;
|
|
}catch(\Exception $e){
|
|
Log::error('Error inesperado al desencriptar los datos: ' . $e->getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static function encryptFields(array $data, array $fields)
|
|
{
|
|
foreach ($fields as $field){
|
|
if(isset($data[$field])){
|
|
$data[$field] = self::encryptData($data[$field]);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public static function decryptFields(array $data, array $fields)
|
|
{
|
|
foreach ($fields as $field){
|
|
if(isset($data[$field])){
|
|
$data[$field] = self::decryptData($data[$field]);
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
}
|