mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-28 17:44:58 +00:00
Implements StoredObjectManager for local storage
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\DocStoreBundle\Service\Cryptography;
|
||||
|
||||
use Base64Url\Base64Url;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use Random\Randomizer;
|
||||
|
||||
class KeyGenerator
|
||||
{
|
||||
private readonly Randomizer $randomizer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->randomizer = new Randomizer();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{alg: string, ext: bool, k: string, key_ops: list<string>, kty: string}
|
||||
*/
|
||||
public function generateKey(string $algo = StoredObjectManagerInterface::ALGORITHM): array
|
||||
{
|
||||
if (StoredObjectManagerInterface::ALGORITHM !== $algo) {
|
||||
throw new \LogicException(sprintf("Algorithm '%s' is not supported.", $algo));
|
||||
}
|
||||
|
||||
$key = $this->randomizer->getBytes(128);
|
||||
|
||||
return [
|
||||
'alg' => 'A256CBC',
|
||||
'ext' => true,
|
||||
'k' => Base64Url::encode($key),
|
||||
'key_ops' => ['encrypt', 'decrypt'],
|
||||
'kty' => 'oct',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int<0, 255>>
|
||||
*/
|
||||
public function generateIv(): array
|
||||
{
|
||||
$iv = [];
|
||||
for ($i = 0; $i < 16; ++$i) {
|
||||
$iv[] = unpack('C', $this->randomizer->getBytes(8))[1];
|
||||
}
|
||||
|
||||
return $iv;
|
||||
}
|
||||
}
|
@@ -18,6 +18,8 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
interface StoredObjectManagerInterface
|
||||
{
|
||||
public const ALGORITHM = 'AES-256-CBC';
|
||||
|
||||
/**
|
||||
* @param StoredObject|StoredObjectVersion $document if a StoredObject is given, the last version will be used
|
||||
*/
|
||||
|
Reference in New Issue
Block a user