assistenzde/simple-cryptographic-bundle

一个简单的加密服务,使用openssl_encrypt方法(如aes128、aes256、blowfish等加密方法)对输入字符串进行编码/解码。

0.0 2022-07-01 13:13 UTC

This package is auto-updated.

Last update: 2024-09-29 05:57:15 UTC


README

Latest Stable Version Total Downloads License PHP ≥v7.4 Symfony ≥5

SimpleCryptographicService 类包含用于使用对称加密对短语进行加密/解密的方法的类。使用 OpenSSL,相同的短语每次编码的结果都会产生不同的编码字符串。

快速使用示例

$cryptographicService = new SimpleCryptographicService('$ecr3t');

$encryptedString1 = $cryptographicService->encrypt('Hello world!');  // some encrypted string, i.e. 'ABCDEFG'
echo $cryptographicService->decrypt($encryptedString1);              // outputs 'Hello world!'

$encryptedString2 = $cryptographicService->encrypt('Hello world!');  // some encrypted string, i.e. 'HIJKLMNOP'
echo $cryptographicService->decrypt($encryptedString2);              // outputs 'Hello world!'

此库可以轻松地用于Symfony项目,也可以用于非Symfony项目。

目录

要求

建议使用 PHP ≥ v7.4Symfony ≥ 5

安装

请通过 composer 进行安装。

composer require assistenzde/simple-cryptographic-bundle

该包将自动添加到您的 bundles.yaml 配置文件中。

Symfony配置

默认情况下,该包依赖于 APP_SECRET 环境变量,并使用 aes-256-ctr 加密方法。如果您想使用自定义密钥或自定义加密方法,请创建 config/packages/simple-cryptographic-bundle.yaml 配置文件并设置相关值。

simple-cryptographic-bundle.yaml:

simple-cryptographic-bundle.yaml:
  # set a custom cipher key or comment out to use the default value
  # default is %kernel.secret% (which contains the APP_SECRET value)
  key: My-cu5t0m-c1ph3r-k3y
  # set a custom cipher method or comment out to use the default value
  # default is aes-256-ctr
  cipher: camellia-128-ofb

使用

使用依赖注入来访问加密服务。

<?php

namespace MyNamespace;

use Assistenzde\SimpleCryptographicBundle\Service\SimpleCryptographicService;

/**
 * Class AuthenticationSubscriber
 */
final class MyClass
{
    /**
     * the crypto service to encode/dedoce strings
     * 
     * @var SimpleCryptographicService
     */
    protected SimpleCryptographicService $simpleCryptographicService;

    /**
     * MyClass constructor
     * 
     * @param SimpleCryptographicService $simpleCryptographicService
     */
    public function __construct(SimpleCryptographicService $simpleCryptographicService)
    {       
        $this->simpleCryptographicService = $simpleCryptographicService;
    }

    /**
     * 
     */
    public function doSomething()
    {       
         // do some calculation and get a user token
         
         // encrypt the user token        
         $encryptedUserToken = $this->simpleCryptographicService->encrypt($userToken);
         
         // and do some more stuff,
         // esp. use the encrypted token (i.e. as request parameter or to save in a file)

         // derypt the encrypted stuff        
         $userToken = $this->simpleCryptographicService->decrypt($encryptedUserToken); 
    }
}

我们建议使用以下公共方法

  • SimpleCryptographicService::encrypt
  • SimpleCryptographicService::decrypt.

为了临时使用自定义设置,切换到静态方法

  • SimpleCryptographicService::EncryptWithMethod 和/或
  • SimpleCryptographicService::DecryptWithMethod.

鸣谢

非常感谢 stackoverflow 用户 Scott Arciszewskihttps://stackoverflow.com/a/30189841/4351778 答案中的解释。