Conditions | 3 |
Paths | 4 |
Total Lines | 55 |
Lines | 55 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
107 | View Code Duplication | 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 | |||
164 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.