refkinscallv / crypto
PHP 加密库
1.0.1
2024-08-18 09:10 UTC
README
Crypto
类提供了加密和解密功能,支持两种存储方法:本地文件存储和数据库存储。本文档解释了如何使用 Crypto
类以及如何自定义其配置。
基本用法
本地文件存储
<?php use RF\Crypto\Crypto; // Instantiate Crypto with local file storage $crypt = new Crypto([ "encryptKey" => "your-secret-key", "encryptFile" => "/path/to/encrypt.txt", "encryptLimitLine" => 5000, // Optional: Max lines to keep in the file "encryptCipher" => "AES-256-CBC", // Optional: Encryption cipher "encryptStoreMethod" => "local" // Storage method ]); // Set a custom file path // $crypt->setFile("/path/to/anotherFile.txt"); // Encrypt data $encryptedHash = $crypt->encrypt("Sensitive Data"); // $encryptedHash = $crypt->encrypt([1, 2, 3], "array"); if array // Decrypt data $decryptedData = $crypt->decrypt($encryptedHash); // $decryptedData = $crypt->decrypt($encryptedHash, "array"); if array ?>
数据库存储
<?php use RF\Crypto\Crypto; // Instantiate Crypto with database storage $crypt = new Crypto([ "encryptKey" => "your-secret-key", "encryptCipher" => "AES-256-CBC", // Optional: Encryption cipher "encryptStoreMethod" => "database", // Storage method "encryptDBHandler" => function($data, $mode) { global $db; // Assume $db is a database connection object if ($mode === "write") { // Example: Insert encrypted data into the database $rawData = explode(":", $data); $query = "INSERT INTO table_encrypt (column_md5, column_actual_encrypt) VALUES (?, ?)"; $stmt = $db->prepare($query); $stmt->execute([$rawData[1], $rawData[0]]); return $stmt->rowCount(); } elseif ($mode === "read") { // Example: Retrieve decrypted data from the database $query = "SELECT column_actual_encrypt FROM table_encrypt WHERE column_md5 = ? LIMIT 1"; $stmt = $db->prepare($query); $stmt->execute([$data]); $result = $stmt->fetchColumn(); return $result ? $result : false; } return false; } ]); // Encrypt data $encryptedHash = $crypt->encrypt("Sensitive Data"); // $encryptedHash = $crypt->encrypt([1, 2, 3], "array"); if array // Decrypt data $decryptedData = $crypt->decrypt($encryptedHash); // $decryptedData = $crypt->decrypt($encryptedHash, "array"); if array ?>
类方法
__construct(array $args)
构造函数,用于使用配置设置初始化 Crypto
对象。
-
参数
array $args
: 包含键的配置数组encryptKey
(string): 加密密钥。encryptFile
(string, optional): 本地文件存储路径。encryptLimitLine
(int, optional): 本地文件中保持的最大行数。encryptCipher
(string, optional): 要使用的加密算法。encryptStoreMethod
(string, optional): 存储方法,可以是 'local' 或 'database'。encryptDBHandler
(callable, optional): 用于数据库操作的闭包函数(如果使用 'database' 则必需)。
-
抛出
- 如果配置无效或缺少必需的字段,将抛出
Exception
。
- 如果配置无效或缺少必需的字段,将抛出
setFile(string $filePath): void
设置本地存储的自定义文件路径。
- 参数
string $filePath
: 文件路径。
encrypt(string $data, string $type = "string")
加密提供的数据。
- 参数
string $data
: 要加密的数据。string $type
: 数据类型 ('string' 或 'array')。默认为 'string'。
- 返回
string|false
: 加密数据的 MD5 哈希或失败时返回false
。
decrypt(string $data, string $type = "string")
解密提供的数据。
- 参数
string $data
: 要解密的加密数据。string $type
: 数据类型 ('string' 或 'array')。默认为 'string'。
- 返回
string|array|false
: 解密数据或失败时返回false
。
存储方法
本地文件存储
本地文件存储方法将加密数据写入指定的文件,并处理文件管理,例如维护最大行数。
数据库存储
数据库存储方法使用自定义闭包来处理与数据库的数据操作。闭包必须接受两个参数:$data
(要处理的数据)和$mode
('write' 或 'read')。