Completed
Push — master ( 91fec0...3d39f9 )
by Anthony
05:07
created

AbstractMcryptMixer::test()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * The Mcrypt abstract mixer class
4
 *
5
 * PHP version 5.3
6
 *
7
 * @category   PHPCryptLib
8
 * @package    Random
9
 * @subpackage Mixer
10
 * @author     Anthony Ferrara <[email protected]>
11
 * @copyright  2013 The Authors
12
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version    Build @@version@@
14
 */
15
16
namespace RandomLib;
17
18
/**
19
 * The mcrypt abstract mixer class
20
 *
21
 * @category   PHPCryptLib
22
 * @package    Random
23
 * @subpackage Mixer
24
 * @author     Anthony Ferrara <[email protected]>
25
 * @author     Chris Smith <[email protected]>
26
 */
27
abstract class AbstractMcryptMixer extends AbstractMixer {
28
    /**
29
     * mcrypt module resource
30
     *
31
     * @var resource
32
     */
33
    private $mcrypt;
34
35
    /**
36
     * Block size of cipher
37
     *
38
     * @var int
39
     */
40
    private $blockSize;
41
42
    /**
43
     * Cipher initialization vector
44
     *
45
     * @var string
46
     */
47
    private $initv;
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public static function test() {
53
        return extension_loaded('mcrypt');
54
    }
55
56
    /**
57
     * Construct mcrypt mixer
58
     */
59
    public function __construct() {
60
        $this->mcrypt    = mcrypt_module_open($this->getCipher(), '', MCRYPT_MODE_ECB, '');
61
        $this->blockSize = mcrypt_enc_get_block_size($this->mcrypt);
62
        $this->initv     = str_repeat(chr(0), mcrypt_enc_get_iv_size($this->mcrypt));
63
    }
64
65
    /**
66
     * Performs cleanup
67
     */
68
    public function __destruct() {
69
        if ($this->mcrypt) {
70
            mcrypt_module_close($this->mcrypt);
71
        }
72
    }
73
74
    /**
75
     * Fetch the cipher for mcrypt.
76
     *
77
     * @return string
78
     */
79
    abstract protected function getCipher();
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function getPartSize() {
85
        return $this->blockSize;
86
    }
87
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function mixParts1($part1, $part2) {
93
        return $this->encryptBlock($part1, $part2);
94
    }
95
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function mixParts2($part1, $part2) {
101
        return $this->decryptBlock($part2, $part1);
102
    }
103
104
    /**
105
     * Encrypts a block using the suppied key
106
     *
107
     * @param  string $input Plaintext to encrypt
108
     * @param  string $key   Encryption key
109
     * @return string Resulting ciphertext
110
     */
111 View Code Duplication
    private function encryptBlock($input, $key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
        if (!$input && !$key) {
113
            return '';
114
        }
115
116
        $this->prepareCipher($key);
117
        $result = mcrypt_generic($this->mcrypt, $input);
118
        mcrypt_generic_deinit($this->mcrypt);
119
120
        return $result;
121
    }
122
123
    /**
124
     * Derypts a block using the suppied key
125
     *
126
     * @param  string $input Ciphertext to decrypt
127
     * @param  string $key   Encryption key
128
     * @return string Resulting plaintext
129
     */
130 View Code Duplication
    private function decryptBlock($input, $key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
        if (!$input && !$key) {
132
            return '';
133
        }
134
135
        $this->prepareCipher($key);
136
        $result = mdecrypt_generic($this->mcrypt, $input);
137
        mcrypt_generic_deinit($this->mcrypt);
138
139
        return $result;
140
    }
141
142
    /**
143
     * Sets up the mcrypt module
144
     *
145
     * @param  string $key
146
     * @return void
147
     */
148
    private function prepareCipher($key) {
149
        if (0 !== mcrypt_generic_init($this->mcrypt, $key, $this->initv)) {
150
            throw new \RuntimeException('Failed to prepare mcrypt module');
151
        }
152
    }
153
}
154