Skip to content

Commit

Permalink
modernize package & add support for newer versions of php & msgpack
Browse files Browse the repository at this point in the history
  • Loading branch information
kduma committed Feb 25, 2021
1 parent 552a23f commit 6ac1be7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"authors": [
{
"name": "Krystian Duma",
"email": "git@kduma.tk"
"email": "git@krystian.duma.sh"
}
],
"autoload": {
Expand All @@ -15,6 +15,7 @@
}
},
"require": {
"rybakit/msgpack": "^0.2.1"
"php": ">=7.4",
"rybakit/msgpack": "^0.7.2"
}
}
41 changes: 31 additions & 10 deletions src/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@

namespace KDuma\FileHasher;

use MessagePack\Unpacker;
use Exception;
use MessagePack\MessagePack;

/**
* Class Checker.
*/
class Checker
{
/**
* @param $hash_file
* @param string $hash_file
* @return bool
* @throws \Exception
* @throws Exception
*/
public static function file($hash_file)
public static function file(string $hash_file): bool
{
$path_info = pathinfo($hash_file);

if ($path_info['extension'] != 'ph') {
throw new \Exception('Unsupported hash file!');
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) {
throw new \Exception('Hash file doesn\'t exist!');
throw new Exception('Hash file doesn\'t exist!');
}

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

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

$sha1 = sha1_file($real_file);
$md5 = md5_file($real_file);
Expand All @@ -50,7 +51,7 @@ public static function file($hash_file)
* @param $checksums_string
* @return bool
*/
public static function stream($stream, $checksums_string)
public static function stream($stream, $checksums_string): bool
{
$sha1 = hash_init('sha1');
$md5 = hash_init('md5');
Expand All @@ -63,7 +64,27 @@ public static function stream($stream, $checksums_string)
$sha1 = hash_final($sha1);
$md5 = hash_final($md5);

$hashes = (new Unpacker())->unpack($checksums_string);
$hashes = MessagePack::unpack($checksums_string);

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

return true;
}

/**
* @param string $content
* @param string $checksums_string
*
* @return bool
*/
public static function string(string $content, string $checksums_string): bool
{
$sha1 = sha1($content);
$md5 = md5($content);

$hashes = MessagePack::unpack($checksums_string);

if ($sha1 != $hashes['sha1'] || $md5 != $hashes['md5']) {
return false;
Expand Down
32 changes: 24 additions & 8 deletions src/Hasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace KDuma\FileHasher;

use MessagePack\Packer;
use Exception;
use MessagePack\MessagePack;

/**
* Class Hasher.
Expand All @@ -11,33 +12,35 @@ class Hasher
{
/**
* @param $real_file
*
* @return bool
* @throws \Exception
* @throws Exception
*/
public static function file($real_file)
public static function file($real_file): bool
{
$path_info = pathinfo($real_file);

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

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

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

file_put_contents($hash_file, (new Packer())->pack(['sha1' => $sha1, 'md5' => $md5]));
file_put_contents($hash_file, MessagePack::pack(['sha1' => $sha1, 'md5' => $md5]));

return true;
}

/**
* @param $stream
* @param resource $stream
*
* @return string
*/
public static function stream($stream)
public static function stream($stream): string
{
$sha1 = hash_init('sha1');
$md5 = hash_init('md5');
Expand All @@ -50,6 +53,19 @@ public static function stream($stream)
$sha1 = hash_final($sha1);
$md5 = hash_final($md5);

return (new Packer())->pack(['sha1' => $sha1, 'md5' => $md5]);
return MessagePack::pack(['sha1' => $sha1, 'md5' => $md5]);
}

/**
* @param string $content
*
* @return string
*/
public static function string(string $content): string
{
$sha1 = sha1($content);
$md5 = md5($content);

return MessagePack::pack(['sha1' => $sha1, 'md5' => $md5]);
}
}

0 comments on commit 6ac1be7

Please sign in to comment.