Passed
Pull Request — master (#26)
by Jaime Pérez
02:02
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\RuntimeException;
10
use SimpleSAML\XMLSecurity\Key\AbstractKey;
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\Key\AbstractKey */
20
    private AbstractKey $key;
21
22
    /** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend */
23
    protected EncryptionBackend $backend;
24
25
    /** @var string */
26
    protected string $default_backend;
27
28
    /** @var string */
29
    protected string $algId;
30
31
32
    /**
33
     * Build an encryption algorithm.
34
     *
35
     * Extend this class to implement your own encryptors.
36
     *
37
     * WARNING: remember to adjust the type of the key to the one that works with your algorithm!
38
     *
39
     * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The signing key.
40
     * @param string $algId The identifier of this algorithm.
41
     */
42
    public function __construct(AbstractKey $key, string $algId)
43
    {
44
        Assert::oneOf(
45
            $algId,
46
            static::getSupportedAlgorithms(),
47
            'Unsupported algorithm for ' . static::class,
48
            RuntimeException::class
0 ignored issues
show
Bug introduced by
SimpleSAML\XMLSecurity\E...RuntimeException::class of type string is incompatible with the type Throwable expected by parameter $exception of SimpleSAML\Assert\Assert::oneOf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            /** @scrutinizer ignore-type */ RuntimeException::class
Loading history...
49
        );
50
        $this->key = $key;
51
        $this->algId = $algId;
52
        $this->setBackend(new $this->default_backend());
53
    }
54
55
56
    /**
57
     * @return string
58
     */
59
    public function getAlgorithmId(): string
60
    {
61
        return $this->algId;
62
    }
63
64
65
    /**
66
     * @return \SimpleSAML\XMLSecurity\Key\AbstractKey
67
     */
68
    public function getKey(): AbstractKey
69
    {
70
        return $this->key;
71
    }
72
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function setBackend(?EncryptionBackend $backend): void
78
    {
79
        if ($backend === null) {
80
            return;
81
        }
82
83
        $this->backend = $backend;
84
        $this->backend->setCipher($this->algId);
85
    }
86
87
88
    /**
89
     * Encrypt a given plaintext with the current algorithm and key.
90
     *
91
     * @param string $plaintext The plaintext to encrypt.
92
     *
93
     * @return string The (binary) ciphertext.
94
     */
95
    public function encrypt(string $plaintext): string
96
    {
97
        return $this->backend->encrypt($this->key, $plaintext);
98
    }
99
100
101
    /**
102
     * Decrypt a given ciphertext with the current algorithm and key.
103
     *
104
     * @param string The (binary) ciphertext to decrypt.
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Alg\Encryption\The was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
105
     *
106
     * @return string The decrypted plaintext.
107
     */
108
    public function decrypt(string $ciphertext): string
109
    {
110
        return $this->backend->decrypt($this->key, $ciphertext);
111
    }
112
}