-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hash validation rule for cell values (#104)
This commit introduces the Hash class to the Csv-Blueprint project's rules for cell validation. The new Hash rule validates whether a cell value correctly represents a hash by checking against a variety of supported algorithms. The addition of this rule allows the project to provide a more comprehensive and diverse range of data validation options.
- Loading branch information
Showing
9 changed files
with
226 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,8 @@ columns: | |
is_alnum: true | ||
is_alpha: true | ||
|
||
hash: set_algo | ||
|
||
aggregate_rules: | ||
is_unique: true | ||
is_sorted: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\CsvBlueprint\Rules\Cell; | ||
|
||
class Hash extends AbstractCellRule | ||
{ | ||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
self::getHelpTitle(), | ||
[self::DEFAULT => ['set_algo', 'Example: "1234567890abcdef".']], | ||
]; | ||
} | ||
|
||
public function validateRule(string $cellValue): ?string | ||
{ | ||
if ($cellValue === '') { | ||
return null; | ||
} | ||
|
||
$regex = self::getRegexList(); | ||
$hashAlg = $this->getOptionAsString(); | ||
|
||
if (!isset($regex[$hashAlg])) { | ||
return "The algorithm \"{$hashAlg}\" is not supported."; | ||
} | ||
|
||
if (\preg_match($regex[$hashAlg], $cellValue) === 0) { | ||
return "The value \"<c>{$cellValue}</c>\" is not a valid hash for the " . | ||
"algorithm \"<green>{$hashAlg}</green>\""; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static function getRegex(int $length, string $charset = '[a-f0-9]'): string | ||
{ | ||
return "/^{$charset}{{$length}}\$/i"; | ||
} | ||
|
||
private static function getRegexList(): array | ||
{ | ||
return [ | ||
'md2' => self::getRegex(32), | ||
'md4' => self::getRegex(32), | ||
'md5' => self::getRegex(32), | ||
|
||
'sha1' => self::getRegex(40), | ||
'sha224' => self::getRegex(56), | ||
'sha256' => self::getRegex(64), | ||
'sha384' => self::getRegex(96), | ||
'sha512/224' => self::getRegex(56), | ||
'sha512/256' => self::getRegex(64), | ||
'sha512' => self::getRegex(128), | ||
'sha3-224' => self::getRegex(56), | ||
'sha3-256' => self::getRegex(64), | ||
'sha3-384' => self::getRegex(96), | ||
'sha3-512' => self::getRegex(128), | ||
|
||
'ripemd128' => self::getRegex(32), | ||
'ripemd160' => self::getRegex(40), | ||
'ripemd256' => self::getRegex(64), | ||
'ripemd320' => self::getRegex(80), | ||
|
||
'whirlpool' => self::getRegex(128), | ||
|
||
'tiger128,3' => self::getRegex(32), | ||
'tiger160,3' => self::getRegex(40), | ||
'tiger192,3' => self::getRegex(48), | ||
'tiger128,4' => self::getRegex(32), | ||
'tiger160,4' => self::getRegex(40), | ||
'tiger192,4' => self::getRegex(48), | ||
|
||
'snefru' => self::getRegex(64), | ||
'snefru256' => self::getRegex(64), | ||
|
||
'gost' => self::getRegex(64), | ||
'gost-crypto' => self::getRegex(64), | ||
|
||
'adler32' => self::getRegex(8), | ||
|
||
'crc32' => self::getRegex(8), | ||
'crc32b' => self::getRegex(8), | ||
'crc32c' => self::getRegex(8), | ||
|
||
'fnv132' => self::getRegex(8), | ||
'fnv1a32' => self::getRegex(8), | ||
|
||
'fnv164' => self::getRegex(16), | ||
'fnv1a64' => self::getRegex(16), | ||
|
||
'joaat' => self::getRegex(8), | ||
|
||
'murmur3a' => self::getRegex(8), | ||
'murmur3c' => self::getRegex(32), | ||
'murmur3f' => self::getRegex(32), | ||
|
||
'xxh32' => self::getRegex(8), | ||
'xxh64' => self::getRegex(16), | ||
'xxh3' => self::getRegex(16), | ||
'xxh128' => self::getRegex(32), | ||
|
||
'haval128,3' => self::getRegex(32), | ||
'haval160,3' => self::getRegex(40), | ||
'haval192,3' => self::getRegex(48), | ||
'haval224,3' => self::getRegex(56), | ||
'haval256,3' => self::getRegex(64), | ||
'haval128,4' => self::getRegex(32), | ||
'haval160,4' => self::getRegex(40), | ||
'haval192,4' => self::getRegex(48), | ||
'haval224,4' => self::getRegex(56), | ||
'haval256,4' => self::getRegex(64), | ||
'haval128,5' => self::getRegex(32), | ||
'haval160,5' => self::getRegex(40), | ||
'haval192,5' => self::getRegex(48), | ||
'haval224,5' => self::getRegex(56), | ||
'haval256,5' => self::getRegex(64), | ||
]; | ||
} | ||
|
||
private static function getHelpTitle(): array | ||
{ | ||
$maxOnLine = 10; | ||
$lines = \array_chunk(\array_keys(self::getRegexList()), $maxOnLine); | ||
|
||
$result = ['Check if the value is a valid hash. Supported algorithms:']; | ||
foreach ($lines as $line) { | ||
$result[] = ' - ' . \implode(', ', $line); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* JBZoo Toolbox - Csv-Blueprint. | ||
* | ||
* This file is part of the JBZoo Toolbox project. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT | ||
* @copyright Copyright (C) JBZoo.com, All rights reserved. | ||
* @see https://github.com/JBZoo/Csv-Blueprint | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JBZoo\PHPUnit\Rules\Cell; | ||
|
||
use JBZoo\CsvBlueprint\Rules\Cell\Hash; | ||
use JBZoo\PHPUnit\Rules\TestAbstractCellRule; | ||
use JBZoo\Utils\Str; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
final class HashTest extends TestAbstractCellRule | ||
{ | ||
protected string $ruleClass = Hash::class; | ||
|
||
public function testPositive(): void | ||
{ | ||
$algos = \hash_algos(); | ||
$attempts = 100; | ||
|
||
foreach ($algos as $algo) { | ||
$rule = $this->create($algo); | ||
isSame('', $rule->test('')); | ||
|
||
foreach (\range(1, $attempts) as $i) { | ||
$hash = \hash($algo, Str::random(32)); | ||
$strlen = \strlen($hash); | ||
isSame('', $rule->test($hash), "'{$algo}' => \$this->getRegex({$strlen}),"); | ||
} | ||
} | ||
} | ||
|
||
public function testNegative(): void | ||
{ | ||
$rule = $this->create('qwerty'); | ||
isSame( | ||
'The algorithm "qwerty" is not supported.', | ||
$rule->test('qwerty'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters