geekcell/sodium-bundle

一个用于与PHP的Sodium扩展交互的Symfony扩展包

安装次数: 4,337

依赖关系: 0

建议者: 0

安全性: 0

星星: 1

关注者: 4

分支: 1

开放问题: 1

类型:symfony-bundle

1.0.2 2023-04-18 09:36 UTC

This package is auto-updated.

Last update: 2024-09-28 12:34:29 UTC


README

Unit tests workflow status Coverage Bugs Maintainability Rating Quality Gate Status

一个用于与PHP的Sodium扩展交互的Symfony扩展包。

安装

要使用此包,请使用Composer在您的Symfony项目中添加它。

composer require geekcell/sodium-bundle

请确认在config/bundles.php中已启用该扩展包

<?php

return [
    // other bundles ...
    GeekCell\SodiumBundle\GeekCellSodiumBundle::class => ['all' => true],
];

限制

目前,此扩展包仅支持libsodium的匿名和认证的公钥加密。

配置

创建一个配置文件config/packages/geek_cell_sodium.yaml,在此配置您的加密和解密的基础64编码的公钥和私钥/密钥。强烈建议不要以纯文本形式存储它们,而是从添加到.gitignore文件的.env.local文件中读取。

geek_cell_sodium:
    public_key: '%env(SODIUM_PUBLIC_KEY)%'
    private_key: '%env(SODIUM_PRIVATE_KEY)%'

如果您的应用程序仅计划使用匿名(共享)公钥加密,则只需必填public_key字段。如果需要进行认证和解密,则还必须配置private_key,否则在运行时将抛出异常。

此扩展包附带了sodium:generate-keys控制台命令,用于为您生成一组公钥/私钥。

❯ bin/console sodium:generate-keys
Generating a new set of public and private keys...

Public Key:  cqJZXt1dhZtyYZ0NcOmwkgcyvW2t9w2Wdwe/Wk6zegk=
Private Key: G3XKnSunNpN1LHKY34LFen7XI2dmu6xBk9UeTQIxNwY=

Please add or update the following environment variables in your .env.local file:

SODIUM_PUBLIC_KEY=cqJZXt1dhZtyYZ0NcOmwkgcyvW2t9w2Wdwe/Wk6zegk=
SODIUM_PRIVATE_KEY=G3XKnSunNpN1LHKY34LFen7XI2dmu6xBk9UeTQIxNwY=

Done!

使用方法

只需在您的代码中类型提示GeekCell\SodiumBundle\Sodium\Sodium服务,并使用其encryptdecrypt方法即可。

匿名加密

以下示例演示了使用仅共享公钥的匿名加密。为了解密消息,接收者需要公钥和相应的私钥/密钥。

<?php

// Sender

namespace Alice\Service;

use GeekCell\SodiumBundle\Sodium\Sodium;

class AnonymousEncryptionService
{
    public function __construct(
        private readonly Sodium $sodium,
    ) {}

    public function encryptMessage(string $message): string
    {
        return $this->sodium
            ->with('box')
            ->encrypt($message)
        ;
    }
}
<?php

// Receiver

namespace Bob\Service;

use GeekCell\SodiumBundle\Sodium\Sodium;

class AnonymousDecryptionService
{
    public function __construct(
        private readonly Sodium $sodium,
    ) {}

    public function decryptMessage(string $message): string
    {
        return $this->sodium
            ->with('box')
            ->decrypt($message)
        ;
    }
}

认证加密

或者,您可以使用认证公钥加密,通过使用接收者的公钥和非确定值来加密特定消息。接收者可以使用发送者的公钥和非确定值来解密密文。

// Sender

namespace Alice\Service;

use GeekCell\SodiumBundle\Sodium\Sodium;

class AuthenticatedEncryptionService
{
    public function __construct(
        private readonly Sodium $sodium,
    ) {}

    public function encryptMessage(string $message, string $recipientPublicKey, $string $nonce): string
    {
        return $this->sodium
            ->with('box')
            ->for($recipientPublicKey)
            ->encrypt($message, $nonce)
        ;
    }
}
<?php

// Receiver

namespace Bob\Service;

use GeekCell\SodiumBundle\Sodium\Sodium;

class AuthenticatedDecryptionService
{
    public function __construct(
        private readonly Sodium $sodium,
    ) {}

    public function decryptMessage(string $message, string $senderPublicKey, string $nonce): string
    {
        return $this->sodium
            ->with('box')
            ->from($senderPublicKey)
            ->decrypt($message, $nonce)
        ;
    }
}

外观

对于无法通过Symfony的DIC(例如,如果您想直接加密或解密您的Doctine实体的字段)注入GeekCell\SodiumBundle\Sodium\Sodium的情况,您可以使用container-facade以方便使用。

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use GeekCell\SodiumBundle\Support\Facade\Sodium;

#[ORM\Entity]
class DiaryEntry
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    private ?int $id;

    #[ORM\Column(type: 'datetime')]
    private \DateTimeInterface $date;

    #[Column(type: 'text')]
    private string $encryptedEntry;

    public function setEntry(string $entry): void
    {
        $this->encryptedEntry = Sodium::with('box')->encrypt($entry);
    }

    // ...
}

有关更多信息,请参阅geekcell/container-facade