20250214-1
[Д] [HashGetType]: Добавлен параметр ByHash (для получения класса хеша путём передачи ему уже вычисленного хэша). [О] [FileHash]: __construct - $hashBy по умолчанию теперь HashGetType::ByHash. [Д] [FileHash]: Добавлены функции сериализации и десериализации (теперь класс реализует интерфейс ISerializable). [Д] [FileHash]: Добавлена функции валидации хэша Validate.
This commit is contained in:
parent
b6f9698e59
commit
3aefbd7f27
@ -4,39 +4,35 @@ namespace goodboyalex\php_components_pack\classes;
|
|||||||
|
|
||||||
use goodboyalex\php_components_pack\enums\HashGetType;
|
use goodboyalex\php_components_pack\enums\HashGetType;
|
||||||
use goodboyalex\php_components_pack\extensions\StringExtension;
|
use goodboyalex\php_components_pack\extensions\StringExtension;
|
||||||
|
use goodboyalex\php_components_pack\interfaces\ISerializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Класс для работы с хэшем файла или строки.
|
* Класс для работы с хэшем файла или строки.
|
||||||
*
|
*
|
||||||
* @author Александр Бабаев
|
* @author Александр Бабаев
|
||||||
* @package php_components_pack
|
* @package php_components_pack
|
||||||
* @version 1.0
|
* @version 1.0.1
|
||||||
* @since 1.0.5
|
* @since 1.0.5
|
||||||
*/
|
*/
|
||||||
final class FileHash
|
final class FileHash implements ISerializable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string $Hash Хэш файла.
|
* @var string $Hash Хэш файла.
|
||||||
*/
|
*/
|
||||||
private(set) string $Hash;
|
private(set) string $Hash;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var HashGetType Тип получения хэша.
|
|
||||||
*/
|
|
||||||
public HashGetType $HashBy;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Конструктор.
|
* Конструктор.
|
||||||
*
|
*
|
||||||
* @param string $str Строка или имя файла.
|
* @param string $str Хэщ, строка или имя файла.
|
||||||
* @param HashGetType $HashBy Тип получения хэша.
|
* @param HashGetType $hashBy Тип получения хэша.
|
||||||
*/
|
*/
|
||||||
public function __construct (string $str = "", HashGetType $HashBy = HashGetType::ByString)
|
public function __construct (string $str = "", HashGetType $hashBy = HashGetType::ByHash)
|
||||||
{
|
{
|
||||||
$this->HashBy = $HashBy;
|
$this->Hash = match ($hashBy) {
|
||||||
$this->Hash = match ($HashBy) {
|
|
||||||
HashGetType::ByString => $this->pGetHash($str),
|
HashGetType::ByString => $this->pGetHash($str),
|
||||||
HashGetType::ByFile => $this->pGetFileHash($str)
|
HashGetType::ByFile => $this->pGetFileHash($str),
|
||||||
|
HashGetType::ByHash => $str
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,4 +72,37 @@ final class FileHash
|
|||||||
{
|
{
|
||||||
return StringExtension::Compare($this->Hash, $otherHash->Hash, true) === 0;
|
return StringExtension::Compare($this->Hash, $otherHash->Hash, true) === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Проверяет, совпадает ли хэш с хэшем/файлом/строкой <code>str</code>.
|
||||||
|
*
|
||||||
|
* @param string $str Хэш, строка или имя файла.
|
||||||
|
* @param HashGetType $hashBy Тип получения хэша.
|
||||||
|
*
|
||||||
|
* @return bool <code>true</code>, если совпадают, и <code>false</code>, если не совпадают.
|
||||||
|
*/
|
||||||
|
public function Validate (string $str, HashGetType $hashBy): bool
|
||||||
|
{
|
||||||
|
return match ($hashBy) {
|
||||||
|
HashGetType::ByString => $this->pGetHash($str) == $this->Hash,
|
||||||
|
HashGetType::ByFile => $this->pGetFileHash($str) == $this->Hash,
|
||||||
|
HashGetType::ByHash => $str == $this->Hash,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function Serialize (): string
|
||||||
|
{
|
||||||
|
return serialize($this->Hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function UnSerialize (string $serialized): void
|
||||||
|
{
|
||||||
|
$this->Hash = unserialize($serialized);
|
||||||
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ use goodboyalex\php_components_pack\traits\EnumExtensionsTrait;
|
|||||||
*
|
*
|
||||||
* @author Александр Бабаев
|
* @author Александр Бабаев
|
||||||
* @package php_components_pack
|
* @package php_components_pack
|
||||||
* @version 1.0
|
* @version 1.1
|
||||||
* @since 1.0.5
|
* @since 1.0.5
|
||||||
*/
|
*/
|
||||||
enum HashGetType: int
|
enum HashGetType: int
|
||||||
@ -17,13 +17,18 @@ enum HashGetType: int
|
|||||||
// Подключаю расширение для Enum
|
// Подключаю расширение для Enum
|
||||||
use EnumExtensionsTrait;
|
use EnumExtensionsTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* По строке хэша.
|
||||||
|
*/
|
||||||
|
case ByHash = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* По строке.
|
* По строке.
|
||||||
*/
|
*/
|
||||||
case ByString = 0;
|
case ByString = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* По файлу.
|
* По файлу.
|
||||||
*/
|
*/
|
||||||
case ByFile = 1;
|
case ByFile = 2;
|
||||||
}
|
}
|
@ -12,7 +12,11 @@ class FileHashTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->PrepareForTest();
|
$this->PrepareForTest();
|
||||||
|
|
||||||
$fileHash = new FileHash("Тестовое слово");
|
$fileHash = new FileHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||||
|
$this->assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", $fileHash->Hash);
|
||||||
|
|
||||||
|
|
||||||
|
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||||||
$this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
|
$this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
|
||||||
|
|
||||||
$fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
|
$fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
|
||||||
@ -27,6 +31,7 @@ class FileHashTest extends TestCase
|
|||||||
require_once __DIR__ . '/../../sources/traits/EnumExtensionsTrait.php';
|
require_once __DIR__ . '/../../sources/traits/EnumExtensionsTrait.php';
|
||||||
require_once __DIR__ . '/../../sources/enums/HashGetType.php';
|
require_once __DIR__ . '/../../sources/enums/HashGetType.php';
|
||||||
require_once __DIR__ . '/../../sources/extensions/StringExtension.php';
|
require_once __DIR__ . '/../../sources/extensions/StringExtension.php';
|
||||||
|
require_once __DIR__ . '/../../sources/interfaces/ISerializable.php';
|
||||||
require_once __DIR__ . '/../../sources/classes/FileHash.php';
|
require_once __DIR__ . '/../../sources/classes/FileHash.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,9 +39,49 @@ class FileHashTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->PrepareForTest();
|
$this->PrepareForTest();
|
||||||
|
|
||||||
$fileHash = new FileHash("Тестовое слово");
|
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||||||
|
|
||||||
$this->assertTrue($fileHash->IsEqual(new FileHash("Тестовое слово")));
|
$this->assertTrue($fileHash->IsEqual(new FileHash("Тестовое слово", HashGetType::ByString)));
|
||||||
$this->assertFalse($fileHash->IsEqual(new FileHash("Тестовое слово2")));
|
$this->assertFalse($fileHash->IsEqual(new FileHash("Тестовое слово2", HashGetType::ByString)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSerialize ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||||||
|
|
||||||
|
$serialized = $fileHash->Serialize();
|
||||||
|
|
||||||
|
$this->assertEquals("s:64:\"000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7\";", $serialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnSerialize ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$fileHash = new FileHash();
|
||||||
|
|
||||||
|
$serialized = "s:64:\"000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7\";";
|
||||||
|
|
||||||
|
$fileHash->UnSerialize($serialized);
|
||||||
|
|
||||||
|
$this->assertEquals("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7", $fileHash->Hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidate ()
|
||||||
|
{
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$fileHash = new FileHash("Тестовое слово", HashGetType::ByString);
|
||||||
|
|
||||||
|
$this->assertTrue($fileHash->Validate("Тестовое слово", HashGetType::ByString));
|
||||||
|
$this->assertTrue($fileHash->Validate("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7",
|
||||||
|
HashGetType::ByHash));
|
||||||
|
$this->assertFalse($fileHash->Validate("000e22f7ba01ae35f781bc3069038110c46593306cafef6b489f7c83b34629b7",
|
||||||
|
HashGetType::ByString));
|
||||||
|
|
||||||
|
$fileHash = new FileHash(__DIR__ . '/../data/A.php', HashGetType::ByFile);
|
||||||
|
$this->assertTrue($fileHash->Validate(__DIR__ . '/../data/A.php', HashGetType::ByFile));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user