- /* Create block. */
- $p .= $id;
- $h = fopen ($p, 'w');
- $fill = str_repeat ("\0", 1024);
- for ($cnt = 0; $cnt < $size; $cnt += 1024)
- {
- if ($size - $cnt < 1024)
- $fill = str_repeat ("\0", $size - $cnt);
- if (fwrite ($h, $fill) === false)
- {
- fclose ($h);
- jirafeau_block_delete_ ($id);
- return "Error";
- }
- }
- fclose ($h);
-
- /* Generate a write/delete code. */
- $code = jirafeau_gen_random (12);
-
- /* Add block infos. */
- if (file_put_contents ($p . '_infos', date ('U') . NL . $size . NL . $code) === FALSE)
+/**
+ * Decrypt file.
+ * @param $fp_src file path to the file to decrypt.
+ * @param $fp_dst file path to the file to write decrypted file (could be the same).
+ * @param $k string composed of the key and the iv separated by a point ('.')
+ * @return key used to decrypt. a string of length 0 is returned if failed.
+ */
+function
+jirafeau_decrypt_file ($fp_src, $fp_dst, $k)
+{
+ $fs = filesize ($fp_src);
+ if ($fs === false || $fs == 0 || extension_loaded('mcrypt') == false)
+ return false;
+
+ /* Init module */
+ $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
+ /* Extract key and iv. */
+ $crypt_key = $k;
+ $md5_key = md5($crypt_key);
+ $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m));
+ /* Decrypt file. */
+ $r = fopen ($fp_src, 'r');
+ $w = fopen ($fp_dst, 'c');
+ while (!feof ($r))