定制 miladrahimi/phpcrypt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

miladrahimi/phpcrypt

Composer 安装命令:

composer require miladrahimi/phpcrypt

包简介

Encryption, decryption, and hashing tools for PHP projects

README 文档

README

Latest Stable Version Total Downloads Build Status Coverage Status License

PhpCrypt

PhpCrypt is a package for encryption, decryption, and hashing data in PHP projects. It provides an easy-to-use and fluent interface.

Features:

  • Symmetric encryption/decryption using AES and other symmetric methods.
  • Asymmetric encryption/decryption using the RSA method.
  • Hashing and verifying data (e.g. passwords) using the BCrypt method.

Versions

  • v5.x.x
  • v4.x.x
  • v3.x.x (Unsupported)
  • v2.x.x (Unsupported)
  • v1.x.x (Unsupported)

Installation

Install Composer and run the following command in your project's root directory:

composer require miladrahimi/phpcrypt "5.*"

Symmetric Encryption

This example shows how to encrypt and decrypt data using symmetric algorithms like AES.

use MiladRahimi\PhpCrypt\Symmetric;

$symmetric = new Symmetric();
$encryptedData = $symmetric->encrypt('secret');
echo $symmetric->decrypt($encryptedData); // secret

It generates a random key and uses aes-256-cbc method for encrypting/decrypting data.

Custom Key

If you have already a key, you can use your own key like this:

use MiladRahimi\PhpCrypt\Symmetric;

$key = '1234567890123456';

// Set the key using the constructor
$symmetric = new Symmetric($key);

// Or set the key using the setter
$symmetric = new Symmetric();
$symmetric->setKey($key);

// And get the key using the getter
$myKey = $symmetric->getKey();

The method generateKey can help you to generate a new random key. See the snippet below.

use MiladRahimi\PhpCrypt\Symmetric;

$key = Symmetric::generateKey();

Custom Methods

In default, The Symmetric class uses aes-256-cbc method to encrypt/decrypt data. You can use your preferred method as well. See the following example.

use MiladRahimi\PhpCrypt\Exceptions\MethodNotSupportedException;
use MiladRahimi\PhpCrypt\Symmetric;

try {
    $symmetric = new Symmetric();
    $symmetric->setMethod('aria-256-ctr');
    // ...
} catch (MethodNotSupportedException $e) {
    // The method is not supported.
}

Supported Methods

If you want to know which methods do your installed OpenSSL extension support, see the snippet below:

use MiladRahimi\PhpCrypt\Symmetric;

print_r(Symmetric::supportedMethods());

RSA Encryption

RSA is a popular asymmetric encryption/decryption algorithm. The examples below illustrate how to encrypt/decrypt data using the RSA algorithm.

Encryption with private key

In this example, we encrypt data with a private key and decrypt it with the related public key.

use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

$result = $privateRsa->encrypt('secret');
echo $publicRsa->decrypt($result); // secret

Encryption with public key

In this example, we encrypt data with a public key and decrypt it with the related private key.

use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

$result = $publicRsa->encrypt('secret');
echo $privateRsa->decrypt($result); // secret

Base64 Encoding

In default, the encrypted data returned by PrivateRsa::encrypt() and PublicRsa::encrypt() methods will be Base64 encoded. You can disable this encoding like the example below.

use MiladRahimi\PhpCrypt\PrivateRsa;
use MiladRahimi\PhpCrypt\PublicRsa;

$privateRsa = new PrivateRsa('private_key.pem');
$publicRsa = new PublicRsa('public_key.pem');

// Disable Base64 encoding for public encryption
$result = $publicRsa->encrypt('secret', false);

// Disable Base64 encoding for private encryption
$result = $privateRsa->encrypt('secret', false);

Hashing

This example shows how to hash data and verify it.

use MiladRahimi\PhpCrypt\Hash;

$hash = new Hash();

$hashedPassword = $hash->make('MyPassword');
echo $hash->verify('MyPassword', $hashedPassword); // true
echo $hash->verify('AnotherPassword', $hashedPassword); // false

Error Handling

The Symmetric, PrivateRsa, PublicRsa, and Hash classes may throw these exceptions:

  • EncryptionException: When it cannot encrypt data.
  • DecryptionException: When it cannot decrypt data.
  • HashingException: When it cannot hash data.
  • MethodNotSupportedException: When the passed encryption method to the Symmetric class is not supported.
  • InvalidKeyException: When the passed key to PrivateRsa or PublicRsa classes is not valid.

License

PhpCrypt is initially created by Milad Rahimi and released under the MIT License.

miladrahimi/phpcrypt 适用场景与选型建议

miladrahimi/phpcrypt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.73k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2015 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cryptography」 「password」 「hash」 「crypt」 「rsa」 「aes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 miladrahimi/phpcrypt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 miladrahimi/phpcrypt 我们能提供哪些服务?
定制开发 / 二次开发

基于 miladrahimi/phpcrypt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 74.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 33
  • 点击次数: 32
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 31
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-10