helium/encryption-client

此包最新版本(1.0.4)没有提供许可证信息。

连接加密服务的客户端

1.0.4 2020-06-30 18:17 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:11 UTC


README

此库详细介绍了如何连接到加密微服务以加密和解密文本。此库旨在与以下微服务一起使用

安装

可以使用以下任一方式安装此库

  1. 下载

  2. 使用 git 克隆

  3. 通过 composer 安装

如何使用(Laravel 项目)

除了下面的基本加密类之外,Laravel 项目还可以利用一组有用的工具,以便更容易且无痕地使用此加密包。

配置

尽管此库包括默认配置值,但您应提供自己的配置设置,以选择连接到加密微服务时将使用的宿主、算法和密码。

示例 1

只需在您的项目 .env 文件中设置所选值。

ENCRYPTION_HOST=ws://encryption.mywebsite.com
ENCRYPTION_ALGORITHM=aes-256-cbc
ENCRYPTION_PASSWORD=${APP_KEY}

示例 2

对于更自定义的配置,您可以将包配置文件发布并手动编辑值。

警告:出于安全原因,您绝对不应该在配置文件中直接包含敏感信息(例如,encryption.password 值),因为它会直接存储在您的项目仓库中。要使用自定义配置,您不应修改 encryption.password 条目,因为它依赖于未跟踪的 .env 文件(请参阅上面的示例 1)。

php artisan vendor:publish --provider="Helium\Encryption\Providers\EncryptionServiceProvider"
<?php

return [
    'host' => env('ENCRYPTION_HOST', 'ws://:7050'),
    'algorithm' => env('ENCRYPTION_ALGORITHM', 'aes-256-cbc'),
    'password' => env('ENCRYPTION_PASSWORD') //DO NOT CHANGE!
];

单例

示例 1

默认情况下,一个已实例化和连接的单例将绑定到项目服务容器,并可以直接使用提供的 encrypt()decrypt() 函数。

use Helium\Encryption\Encryption;

$encrypted = app(Encryption::class)->encrypt('abc123');
$decrypted = app(Encryption::class)->decrypt($encrypted);

示例 2

此外,一个全局辅助函数允许您无需手动从服务容器解析即可访问单例。

$encrypted = encryption()->encrypt('abc123');
$decrupted = encryption()->encrypt($encrypted);

Eloquent 模型特性

对于 Eloquent 模型,EncryptsAttributes 特性允许您在数据进入和离开数据库时加密和解密数据,而无需手动引用加密客户端。只需在模型类上设置 $encrypedAttributes 数组,以指定哪些属性应在数据库级别进行加密。

示例

use Helium\Encryption\Traits\EncryptsAttributes;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use EncryptsAttributes;

    $fillable = [
        'first_name',
        'last_name',
        'social_security_number'
    ];

    $encryptedAttributes = [
        'social_security_number'
    ];
}
代码
User::create([
    'first_name' => 'George',
    'last_name' => 'Burdell',
    'social_security_number' => '123456789'
]);

$user = User::first();

echo $user->social_security_number;
// 123456789
数据库
first_name | last_name | social_security_number
------------------------------------------------
George     | Burdell   | 9560f15de26a31606f3a7541df05efb1

如何使用(非 Laravel 项目)

当代码被下载或克隆到项目中时,您可以使用引擎访问加密库。您需要知道以下参数

  • host: 连接微服务时的宿主。由于它是一个 WebSocket,通常将是 ws://host:7050 以通过端口 7050 连接到 WebSocket。

  • content: 您想要加密/解密的文本字符串

  • password: 可选密码,您可以使用它进行加密/解密

  • algorithm: 可选算法,用于加密

以下演示了库的不同使用方式。

示例 1

<?php
use Helium\Encryption\Engines\TextTalkEngine;
use Helium\Encryption\Encryption;

$host = 'ws://:7050';
$password = 'somepassword'; //Optional
$algorithm = 'aes-128-cbc'; //Optional

$client = new Encryption(new TextTalkEngine($host, $algorithm, $password));
$client->connect();

$encrypted_content = $client->encrypt("ABC 123");
echo "Encrytped Content: " . $encrypted_content . "\n";
$decrypted_content = $client->decrypt($encrypted_content);
echo "Decrypted Content: " . $decrypted_content . "\n";

$client->close();

示例 2

<?php
use Helium\Encryption\Engines\TextTalkEngine;
use Helium\Encryption\Encryption;

//Example 2
$host = 'ws://:7050';
$password = 'somepassword'; //Optional
$algorithm = 'aes-128-cbc'; //Optional

$client = new Encryption(new TextTalkEngine());
$client->setHost($host);
$client->setAlgorithm($algorithm);
$client->setPassword($password);
$client->connect();

$encrypted_content = $client->encrypt("Doe Rae Me");
echo "Encrytped Content: " . $encrypted_content . "\n";
$decrypted_content = $client->decrypt($encrypted_content);
echo "Decrypted Content: " . $decrypted_content . "\n";

$client->close();

示例 3

use Helium\Encryption\Engines\TextTalkEngine;
use Helium\Encryption\Encryption;

$host = 'ws://:7050';
$password = 'somepassword'; //Optional
$algorithm = 'aes-128-cbc'; //Optional

$client = new Encryption(new TextTalkEngine());
$client->setHost($host);
$client->connect();

$encrypted_content = $client->encrypt("Jumanji", $algorithm, $password);
echo "Encrytped Content: " . $encrypted_content . "\n";
$decrypted_content = $client->decrypt($encrypted_content, $algorithm, $password);
echo "Decrypted Content: " . $decrypted_content . "\n";

$client->close();

带有错误处理的示例

<?php
use Helium\Encryption\Exceptions\EncryptionError;
use Helium\Encryption\Engines\TextTalkEngine;
use Helium\Encryption\Encryption;

$host = 'ws://:7050';

$client = new Encryption(new TextTalkEngine($host));
$client->connect();

try {
    $encrypted_content = $client->encrypt('');
} catch(EncryptionError $e) {
    echo $e->getMessage() . "\n";;
}