Passed
Branch staging (6d4670)
by Tony Karavasilev (Тони
04:18
created

Rc4::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 10
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * The RC4-128 encryption algorithm class.
5
 */
6
7
namespace CryptoManana\SymmetricEncryption;
8
9
use CryptoManana\Core\Abstractions\MessageEncryption\AbstractStreamCipherAlgorithm as SymmetricStreamCipherAlgorithm;
10
11
/**
12
 * Class Rc4 - The RC4-128 encryption algorithm object.
13
 *
14
 * @package CryptoManana\SymmetricEncryption
15
 */
16
class Rc4 extends SymmetricStreamCipherAlgorithm
17
{
18
    /**
19
     * The internal name of the algorithm.
20
     */
21
    const ALGORITHM_NAME = 'RC4';
22
23
    /**
24
     * The internal secret key size measured in raw bytes length for the algorithm
25
     *
26
     * @note For the current algorithm: 128 bits (16 bytes)
27
     */
28
    const KEY_SIZE = 16;
29
30
    /**
31
     * Flag to force native code polyfill realizations, if available.
32
     *
33
     * @var bool Flag to force native realizations.
34
     */
35
    protected $useNative = false;
36
37
    /**
38
     * Stream cipher algorithm constructor.
39
     *
40
     * @codeCoverageIgnore
41
     */
42
    public function __construct()
43
    {
44
        if (in_array(strtolower(static::ALGORITHM_NAME), openssl_get_cipher_methods(), true)) {
45
            parent::__construct();
46
        } else {
47
            $this->useNative = true;
48
49
            /**
50
             * {@internal Serialization and initialization purposes for the default key. }}
51
             */
52
            if (strlen($this->key) < static::KEY_SIZE) {
53
                $this->key = str_pad($this->key, static::KEY_SIZE, "\x0", STR_PAD_RIGHT);
54
            }
55
        }
56
    }
57
58
    /**
59
     * Encrypts the given plain data.
60
     *
61
     * @param string $plainData The plain input string.
62
     *
63
     * @return string The cipher/encrypted data.
64
     * @throws \Exception Validation errors.
65
     */
66 11
    public function encryptData($plainData)
67
    {
68 11
        if (!$this->useNative) {
69
            // @codeCoverageIgnoreStart
70
            return parent::encryptData($plainData);
71
            // @codeCoverageIgnoreEnd
72
        } else {
73 2
            $this->validatePlainDataForEncryption($plainData);
74
75 2
            $plainData = ($plainData === '') ? ' ' : $plainData;
76
77 2
            $cipherData = \CryptoManana\Compatibility\NativeRc4::encryptData($this->key, $plainData);
78
79 2
            return $this->changeOutputFormat($cipherData, true);
80
        }
81
    }
82
83
    /**
84
     * Decrypts the given cipher data.
85
     *
86
     * @param string $cipherData The encrypted input string.
87
     *
88
     * @return string The decrypted/plain data.
89
     * @throws \Exception Validation errors.
90
     */
91 9
    public function decryptData($cipherData)
92
    {
93 9
        if (!$this->useNative) {
94
            // @codeCoverageIgnoreStart
95
            return parent::decryptData($cipherData);
96
            // @codeCoverageIgnoreEnd
97
        } else {
98 2
            $this->validateCipherDataForDecryption($cipherData);
99
100 2
            $cipherData = $this->changeOutputFormat($cipherData, false);
101
102 2
            return \CryptoManana\Compatibility\NativeRc4::decryptData($this->key, $cipherData);
103
        }
104
    }
105
}
106