jf/crypto

文本和文件的加密和解密

1.0.0 2024-03-02 18:18 UTC

This package is auto-updated.

Last update: 2024-10-01 00:09:24 UTC


README

文本和文件的加密和解密。

安装

Composer

本项目使用 Composer 作为依赖管理器,可以按照项目官方文档中的说明进行安装(文档)

要使用此包管理器安装 jf/crypto 包,需要执行

composer require jf/crypto

版本控制

本项目可以使用 git 安装。首先需要克隆项目,然后安装依赖项

git clone git@gitlab.com:joaquinfq/jfCrypto.git
cd jfCrypto
composer install

可用文件

名称描述
jf\Crypto\ACipher用于简单加密数据库或配置文件的类。
jf\Crypto\Assert由包抛出的异常。
jf\Crypto\AsymmetricKeys管理使用非对称密钥的加密和解密需求。
jf\Crypto\Cipher用于简单加密文本或数据的类。
jf\Crypto\FileCipher用于简单加密数据库或配置文件的类。

接口

名称描述
jf\Crypto\IAsymmetricKeys管理非对称加密密钥的类的接口。
jf\Crypto\ICipher用于加密/解密文本的接口。
jf\Crypto\ICrypto管理加密密钥的类的接口。
jf\Crypto\ICryptoId需要加密其标识符的类的接口。
jf\Crypto\IFileCipher用于加密/解密文件的接口。

特性

名称描述
jf\Crypto\TId用于应用到需要加密其数字标识符的对象的特性行为。

示例

jf\Crypto\AssymetricKeys

// Si no tenemos las claves las creamos
// AsymmetricKeys::createKeys(__DIR__);

// Al crear la clase especificamos el directorio donde se encuentran las claves.
$ecdsa     = new AsymmetricKeys(__DIR__);
$content   = file_get_contents(__FILE__);
$encrypted = $ecdsa->encrypt($content);
assert($ecdsa->decrypt($encrypted) === $content);

jf\Crypto\FileCipher

$content    = file_get_contents(__FILE__);
$key        = md5($content); // Asignamos la clave que queremos usar.
$cipher     = new Cipher($key);
$filecipher = new FileCipher($key);
$encrypted  = $cipher->encrypt($content);
$encfile    = __FILE__ . '.encrypted';

assert($cipher->decrypt($encrypted) === $content);
assert($filecipher->save($encfile, $content) === strlen($encrypted));
assert($filecipher->load($encfile) === $content);

unlink($encfile);