Code Duplication    Length = 55-67 lines in 2 locations

func/crypt.php 1 location

@@ 107-161 (lines=55) @@
104
 * @param	string	$mode_directory			Same as mcrypt_module_open().
105
 * @return	string
106
*/
107
function McryptSmplIvProcess($i_action, $s_data, $s_key, $algorithm, $algorithm_directory = '', $mode = 'cfb', $mode_directory = '') {
108
	/* Open the cipher */
109
	$td = mcrypt_module_open($algorithm,
110
		$algorithm_directory,
111
		$mode,
112
		$mode_directory);
113
114
	$ks = mcrypt_enc_get_key_size($td);
115
116
	/* Create key */
117
	$key = substr(sha1($s_key), 0, $ks);
118
119
	// The IV must be unique and must be the same when decrypting/encrypting.
120
121
	// But encrypt/decrypt are executed on 2 machine,
122
	// randon IV will cause decrypt wrong result
123
	// Bad offical example, all put encrypt/decrypt together !
124
125
	/* Create the IV and determine the keysize length, use MCRYPT_RAND
126
	 * on Windows instead */
127
/*
128
	if (true == NixOs())
129
		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
130
	else
131
		$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
132
*/
133
134
	// So now, I use part/duplicate sha1 value of key as IV
135
	$iv = sha1($key);	// Sha1 again :-)
136
	$l_sha1 = strlen($iv);
137
	$l_iv = mcrypt_enc_get_iv_size($td);
138
	if ($l_sha1 < $l_iv) {
139
		// Duplicate sha1 value to generate IV
140
		$iv = str_repeat($iv, round($l_iv / $l_sha1) + 1);
141
	}
142
	$iv = substr($iv, 0, $l_iv);
143
144
	/* Intialize encryption */
145
	mcrypt_generic_init($td, $key, $iv);
146
147
	if (0 == $i_action) {
148
		/* Encrypt data */
149
		$encrypted = mcrypt_generic($td, $s_data);
150
	}
151
	else {
152
		/* Decrypt encrypted string */
153
		$encrypted = mdecrypt_generic($td, $s_data);
154
	}
155
156
	/* Terminate decryption handle and close module */
157
	mcrypt_generic_deinit($td);
158
	mcrypt_module_close($td);
159
160
	return($encrypted);
161
} // end of func McryptSmplIvProcess
162
163
?>
164

src/Fwlib/Util/Algorithm/McryptSimpleIv.php 1 location

@@ 149-215 (lines=67) @@
146
     * @param   string  $modeDirectory          Same as mcrypt_module_open()
147
     * @return  string
148
    */
149
    public function process(
150
        $action,
151
        $source,
152
        $key,
153
        $algorithm,
154
        $algorithmDirectory = '',
155
        $mode = 'cfb',
156
        $modeDirectory = ''
157
    ) {
158
        self::checkExtension(true);
159
160
161
        // Open the cipher
162
        $td = mcrypt_module_open(
163
            $algorithm,
164
            $algorithmDirectory,
165
            $mode,
166
            $modeDirectory
167
        );
168
169
        $ks = mcrypt_enc_get_key_size($td);
170
171
        // Create key
172
        $key = substr(sha1($key), 0, $ks);
173
174
175
        /**
176
         *
177
         * The IV must be unique and must be the same when decrypting/encrypting.
178
         *
179
         * But encrypt/decrypt are executed on 2 machine,
180
         * random IV will cause decrypt wrong result
181
         *
182
         * So now, I use part/duplicate sha1 value of key as IV
183
         *
184
         * Bad official example, all put encrypt/decrypt together !
185
         */
186
187
188
        $iv = sha1($key);   // Sha1 again :-)
189
        $lenSha1 = strlen($iv);
190
        $lenIv = mcrypt_enc_get_iv_size($td);
191
        // @codeCoverageIgnoreStart
192
        if ($lenSha1 < $lenIv) {
193
            // Duplicate sha1 value to generate IV
194
            $iv = str_repeat($iv, round($lenIv / $lenSha1) + 1);
195
        }
196
        // @codeCoverageIgnoreEnd
197
        $iv = substr($iv, 0, $lenIv);
198
199
        // Initialize encryption
200
        mcrypt_generic_init($td, $key, $iv);
201
202
        if (0 == $action) {
203
            // Encrypt data
204
            $encrypted = mcrypt_generic($td, $source);
205
        } else {
206
            // Decrypt encrypted string
207
            $encrypted = mdecrypt_generic($td, $source);
208
        }
209
210
        // Terminate decryption handle and close module
211
        mcrypt_generic_deinit($td);
212
        mcrypt_module_close($td);
213
214
        return($encrypted);
215
    }
216
}
217