+
+/** hash file's content
+ * @param $method hash method, see 'file_hash' option. Valid methods are 'md5', 'md5_outside' or 'random'
+ * @param $file_path file to hash
+ * @returns hash string
+ */
+function jirafeau_hash_file($method, $file_path)
+{
+ switch ($method) {
+ case 'md5_outside':
+ return jirafeau_md5_outside($file_path);
+ case 'md5':
+ return md5_file($file_path);
+ case 'random':
+ return jirafeau_gen_random(32);
+ }
+ return md5_file($file_path);
+}
+
+/** hash part of file: start, end and size.
+ * This is a partial file hash, faster but weaker.
+ * @param $file_path file to hash
+ * @returns hash string
+ */
+function jirafeau_md5_outside($file_path)
+{
+ $out = false;
+ $handle = fopen($file_path, "r");
+ if ($handle === false) {
+ return false;
+ }
+ $size = filesize($file_path);
+ if ($size === false) {
+ goto err;
+ }
+ $first = fread($handle, 64);
+ if ($first === false) {
+ goto err;
+ }
+ if (fseek($handle, $size < 64 ? 0 : $size - 64) == -1) {
+ goto err;
+ }
+ $last = fread($handle, 64);
+ if ($last === false) {
+ goto err;
+ }
+ $out = md5($first . $last . $size);
+ err:
+ fclose($handle);
+ return $out;
+}
+