From 6ac1be7c1920b23ded4078eb8d7c9014729bd875 Mon Sep 17 00:00:00 2001 From: Krystian Duma Date: Thu, 25 Feb 2021 20:26:09 +0100 Subject: [PATCH] modernize package & add support for newer versions of php & msgpack --- composer.json | 5 +++-- src/Checker.php | 41 +++++++++++++++++++++++++++++++---------- src/Hasher.php | 32 ++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 25c40f0..5b607e8 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "authors": [ { "name": "Krystian Duma", - "email": "git@kduma.tk" + "email": "git@krystian.duma.sh" } ], "autoload": { @@ -15,6 +15,7 @@ } }, "require": { - "rybakit/msgpack": "^0.2.1" + "php": ">=7.4", + "rybakit/msgpack": "^0.7.2" } } diff --git a/src/Checker.php b/src/Checker.php index 211d210..e6115d4 100644 --- a/src/Checker.php +++ b/src/Checker.php @@ -2,7 +2,8 @@ namespace KDuma\FileHasher; -use MessagePack\Unpacker; +use Exception; +use MessagePack\MessagePack; /** * Class Checker. @@ -10,30 +11,30 @@ 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); @@ -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'); @@ -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; diff --git a/src/Hasher.php b/src/Hasher.php index be7e08b..1ec5fc8 100644 --- a/src/Hasher.php +++ b/src/Hasher.php @@ -2,7 +2,8 @@ namespace KDuma\FileHasher; -use MessagePack\Packer; +use Exception; +use MessagePack\MessagePack; /** * Class Hasher. @@ -11,10 +12,11 @@ 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); @@ -22,22 +24,23 @@ public static function file($real_file) $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'); @@ -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]); } }