Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kduma committed Dec 2, 2016
2 parents 748f175 + 775d4d9 commit 297ab02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
22 changes: 13 additions & 9 deletions src/Checker.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
namespace KDuma\FileHasher;

namespace KDuma\FileHasher;

use MessagePack\Unpacker;

/**
* Class Checker
* @package KDuma\FileHasher
* Class Checker.
*/
class Checker
{
Expand All @@ -15,29 +14,34 @@ class Checker
* @return bool
* @throws \Exception
*/
public static function file($hash_file) {
public static function file($hash_file)
{
$path_info = pathinfo($hash_file);

if($path_info['extension'] != 'ph')
if ($path_info['extension'] != 'ph') {
throw new \Exception('Unsupported hash file!');
}

$hash_file = realpath($path_info['dirname'].'/'.$path_info['basename']);
$real_file = realpath($path_info['dirname'].'/'.$path_info['filename']);

if(!$hash_file)
if (! $hash_file) {
throw new \Exception('Hash file doesn\'t exist!');
}

if(!$real_file)
if (! $real_file) {
throw new \Exception('Hashed file doesn\'t exist!');
}

$hashes = (new Unpacker())->unpack(file_get_contents($hash_file));

$sha1 = sha1_file($real_file);
$md5 = md5_file($real_file);

if($sha1 != $hashes['sha1'] || $md5 != $hashes['md5'])
if ($sha1 != $hashes['sha1'] || $md5 != $hashes['md5']) {
return false;
}

return true;
}
}
}
13 changes: 7 additions & 6 deletions src/Hasher.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
namespace KDuma\FileHasher;

namespace KDuma\FileHasher;

use MessagePack\Packer;

/**
* Class Hasher
* @package KDuma\FileHasher
* Class Hasher.
*/
class Hasher
{
Expand All @@ -15,14 +14,16 @@ class Hasher
* @return bool
* @throws \Exception
*/
public static function file($real_file) {
public static function file($real_file)
{
$path_info = pathinfo($real_file);

$real_file = realpath($path_info['dirname'].'/'.$path_info['basename']);
$hash_file = $real_file.'.ph';

if(!$real_file)
if (! $real_file) {
throw new \Exception('Hashed file doesn\'t exist!');
}

$sha1 = sha1_file($real_file);
$md5 = md5_file($real_file);
Expand All @@ -31,4 +32,4 @@ public static function file($real_file) {

return true;
}
}
}

0 comments on commit 297ab02

Please sign in to comment.