Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

AbstractEncryptor::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Alg\Encryption;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
9
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
10
use SimpleSAML\XMLSecurity\Key\KeyInterface;
11
12
/**
13
 * An abstract class that implements a generic encryption algorithm
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
abstract class AbstractEncryptor implements EncryptionAlgorithmInterface
18
{
19
    /** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend */
20
    protected EncryptionBackend $backend;
21
22
23
    /**
24
     * Build an encryption algorithm.
25
     *
26
     * Extend this class to implement your own encryptors.
27
     *
28
     * WARNING: remember to adjust the type of the key to the one that works with your algorithm!
29
     *
30
     * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The signing key.
31
     * @param string $algId The identifier of this algorithm.
32
     */
33
    public function __construct(
34
        private KeyInterface $key,
35
        protected string $algId,
36
    ) {
37
        Assert::oneOf(
38
            $algId,
39
            static::getSupportedAlgorithms(),
40
            'Unsupported algorithm for ' . static::class,
41
            UnsupportedAlgorithmException::class,
42
        );
43
        $this->setBackend(new (static::DEFAULT_BACKEND)());
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '(', expecting ':' on line 43 at column 30
Loading history...
44
    }
45
46
47
    /**
48
     * @return string
49
     */
50
    public function getAlgorithmId(): string
51
    {
52
        return $this->algId;
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\XMLSecurity\Key\KeyInterface
58
     */
59
    public function getKey(): KeyInterface
60
    {
61
        return $this->key;
62
    }
63
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function setBackend(?EncryptionBackend $backend): void
69
    {
70
        if ($backend === null) {
71
            return;
72
        }
73
74
        $this->backend = $backend;
75
        $this->backend->setCipher($this->algId);
76
    }
77
78
79
    /**
80
     * Encrypt a given plaintext with the current algorithm and key.
81
     *
82
     * @param string $plaintext The plaintext to encrypt.
83
     *
84
     * @return string The (binary) ciphertext.
85
     */
86
    public function encrypt(string $plaintext): string
87
    {
88
        return $this->backend->encrypt($this->key, $plaintext);
89
    }
90
91
92
    /**
93
     * Decrypt a given ciphertext with the current algorithm and key.
94
     *
95
     * @param string The (binary) ciphertext to decrypt.
96
     *
97
     * @return string The decrypted plaintext.
98
     */
99
    public function decrypt(string $ciphertext): string
100
    {
101
        return $this->backend->decrypt($this->key, $ciphertext);
102
    }
103
}
104