1 Star 0 Fork 0

MoLeft / moyu-cloud-encrypt

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

moyu-cloud-encrypt

本composer包基于 陌屿云加密(encode.phpth.cn) 开发 一个优雅、使用简单、易于拓展的云加密类库

使用方法

使用Composer安装

composer require moleft/moyu-cloud-encrypt

引入autoload

include __DIR__'/vendor/autoload.php';

检查是否支持某种加密方式

use Moyu\CloudEncrypt\Driver;

$result = Driver::support('sg11');
var_dump($result); // true

初始化加密

use Moyu\CloudEncrypt\Driver;

// 设置核心参数
$config = [
    'user'  => '',
    'pass'  => '',
    'apikey'=> ''
];

// 首次使用 Driver 创建加密实例时需要调用 setConfig
$driver = Driver::setConfig($config)->getInstance('sg11');

// 切换加密实例不需要调用 setConfig
$driver = Driver::getInstance('goto');

使用Try...Catch捕获异常

use Moyu\CloudEncrypt\CloudEncryptException;
use Moyu\CloudEncrypt\Driver;

try{
    // 设置核心参数
    $config = [
        'user'  => '',
        'pass'  => '',
        'apikey'=> ''
    ];

    // 创建加密实例
    $driver = Driver::setConfig($config)->getInstance('sg11');

    // TODO 加密具体业务逻辑
    // ...
} catch(CloudEncryptException $e){
    // 获取错误信息
    exit($e->getMessage());
}

内置方法

设置额外参数 setParameter()

// 使用 setParameter($key, $value) 设置键对值
$driver->setParameter('comment', '自定义加密注释');

// 使用 setParameter([key1=>value1,key2=>value2]) 批量设置键对值
$driver->setParameter(['comment'=>'自定义加密注释',...]);

设置加密文件 setFile()

加密文件不能用 setParameter('file',$filePath) 设置

// 直接传入绝对路径或相对路径即可
$driver->setFile($filePath);

开始加密 encrypt()

必须要当配置好所有的参数之后,才可以调用encrypt()加密,如果缺少参数或者加密失败会抛出 CloudEncryptException 异常 这时我们可以用try..catch尝试捕获

$driver->encrypt();

保存加密文件 saveFile()

当加密成功后,可以调用 saveFile($filePath) 方法保存加密好的文件 注意:如果路径不存在,他会自动创建,所以你不需要担心这方面的问题

$driver->saveFile($saveFilePath);

获取加密结果 success()

此方法必须要在调用 saveFile() 之后才可以调用,因为 encrypt() 在加密失败的时候,会抛出异常 如果加密成功调用 success() 返回会布尔值 truefalse

$result = $driver->success();
var_dump($result); // true or false

获取云端返回的消息 message()

有的时候我们可能想要知道云端返回了什么消息,这时可以使用 message() 方法来查看云端的消息

$driver->message();

链式操作

为了优雅与简单共存,所有的内置方法都可以使用链式操作来调用。当然,如果你喜欢的话,甚至可以链式调用 success()message() 尽管这并没有什么用

try {

    // 设置核心参数
    $config = [
        'user'   =>'',
        'pass'   =>'',
        'apikey' =>''
    ];

    // 获取加密实例
    $driver = Driver::setConfig($config)->getInstance('sg11');
    // 第二次使用
    // $driver = Driver::getInstance('sg11');

    // 依次设置 参数 -> 文件 -> 开始加密
    $result = $driver->setParameter(['comment'=>'测试','Ver'=>'72'])
                     ->setFile(__DIR__.'/test.php')
                     ->encrypt()
                     ->saveFile(__DIR__.'/test2.zip');

    // 判断加密结果
    if($result->success()){
        // 获取云端消息
        echo $result->message().'<br>';
        exit('加密成功');
    }else{
        exit('加密失败');
    }

} catch (CloudEncryptException $e) {
    exit($e->getMessage());
}

自定义加密驱动

为了方便后续添加其他的加密驱动,这里提供了自定义加密驱动的方法

1. 在Driver目录下创建驱动文件,格式为[驱动名]Driver.php

比如,我们要创建一个Custom加密,则需要在Driver目录下创建一个叫做CustomDriver.php的文件

2. 命名空间与继承

按照Psr-4命名规范,我们的命名空间必须为 Moyu\CloudEncrypt\Driver 自定义驱动必需要继承自 Moyu\CloudEncrypt\Encrypt

<?php

namespace Moyu\CloudEncrypt\Driver;

use Moyu\CloudEncrypt\Encrypt;

class CustomDriver extends Encrypt
{
    // TODO some code...
}

3. 实现加密功能

由于大部分的功能都已经在 Moyu\CloudEncrypt\Encrypt 中实现,所以我们可以用极少数的代码来完成自定义加密驱动的接入。 $_encrypt_type 是我们自定义驱动的名 重写 parameter() 方法将拓展的参数传入进去,就可以使用我们自定义的加密驱动了

<?php

namespace Moyu\CloudEncrypt\Driver;

use Moyu\CloudEncrypt\Encrypt;

class CustomDriver extends Encrypt
{
    protected $_encrypt_type = 'custom';

    protected function parameter($array = [])
    {
        parent::parameter(['parameter1','parameter2',...]);
    }
}

4. 测试使用

try {

    // 设置核心参数
    $config = [
        'user'   =>'',
        'pass'   =>'',
        'apikey' =>''
    ];

    // 获取加密实例
    $driver = Driver::setConfig($config)->getInstance('custom');

    // 依次设置 参数 -> 文件 -> 开始加密
    $result = $driver->setParameter(['parameter1'=>'custom','parameter2'=>'custom'])
                     ->setFile(__DIR__.'/test.php')
                     ->encrypt()
                     ->saveFile(__DIR__.'/test2.zip');

    // 判断加密结果
    if($result->success()){
        // 获取云端消息
        echo $result->message().'<br>';
        exit('加密成功');
    }else{
        exit('加密失败');
    }

} catch (CloudEncryptException $e) {
    exit($e->getMessage());
}
MIT License Copyright (c) 2022 MoLeft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

moyu-cloud-encryp可以通过Composer添加到你的项目中,为你的项目提供快捷简单的php文件加密服务 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/moleft/moyu-cloud-encrypt.git
git@gitee.com:moleft/moyu-cloud-encrypt.git
moleft
moyu-cloud-encrypt
moyu-cloud-encrypt
master

搜索帮助