diff --git a/4dev/tests/Create/CoreLibsCreateHashTest.php b/4dev/tests/Create/CoreLibsCreateHashTest.php
index fcab739e..4eb7e694 100644
--- a/4dev/tests/Create/CoreLibsCreateHashTest.php
+++ b/4dev/tests/Create/CoreLibsCreateHashTest.php
@@ -114,6 +114,22 @@ final class CoreLibsCreateHashTest extends TestCase
];
}
+ /**
+ * Undocumented function
+ *
+ * @return array
+ */
+ public function hashStandardProvider(): array
+ {
+ $hash_source = 'Some String Text';
+ return [
+ 'Long Hash check: ' . \CoreLibs\Create\Hash::STANDARD_HASH => [
+ $hash_source,
+ hash(\CoreLibs\Create\Hash::STANDARD_HASH, $hash_source)
+ ],
+ ];
+ }
+
/**
* Undocumented function
*
@@ -136,9 +152,13 @@ final class CoreLibsCreateHashTest extends TestCase
/**
* Undocumented function
*
+ * phpcs:disable Generic.Files.LineLength
* @covers ::__sha1Short
+ * @covers ::__crc32b
+ * @covers ::sha1Short
* @dataProvider sha1ShortProvider
- * @testdox __sha1Short $input will be $expected (crc32b) and $expected_sha1 (sha1 short) [$_dataName]
+ * @testdox __sha1Short/__crc32b/sha1short $input will be $expected (crc32b) and $expected_sha1 (sha1 short) [$_dataName]
+ * phpcs:enable Generic.Files.LineLength
*
* @param string $input
* @param string $expected
@@ -149,16 +169,29 @@ final class CoreLibsCreateHashTest extends TestCase
// uses crc32b
$this->assertEquals(
$expected,
- \CoreLibs\Create\Hash::__sha1Short($input)
+ \CoreLibs\Create\Hash::__sha1Short($input),
+ '__sha1Short depreacted'
);
$this->assertEquals(
$expected,
- \CoreLibs\Create\Hash::__sha1Short($input, false)
+ \CoreLibs\Create\Hash::__sha1Short($input, false),
+ '__sha1Short (false) depreacted'
+ );
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::__crc32b($input),
+ '__crc32b'
);
// sha1 type
$this->assertEquals(
$expected_sha1,
- \CoreLibs\Create\Hash::__sha1Short($input, true)
+ \CoreLibs\Create\Hash::__sha1Short($input, true),
+ '__sha1Short (true) depreacted'
+ );
+ $this->assertEquals(
+ $expected_sha1,
+ \CoreLibs\Create\Hash::sha1Short($input),
+ 'sha1Short'
);
}
@@ -166,8 +199,10 @@ final class CoreLibsCreateHashTest extends TestCase
* Undocumented function
*
* @covers ::__hash
+ * @covers ::hashShort
+ * @covers ::hashShort
* @dataProvider hashProvider
- * @testdox __hash $input with $hash_type will be $expected [$_dataName]
+ * @testdox __hash/hashShort/hash $input with $hash_type will be $expected [$_dataName]
*
* @param string $input
* @param string|null $hash_type
@@ -179,12 +214,24 @@ final class CoreLibsCreateHashTest extends TestCase
if ($hash_type === null) {
$this->assertEquals(
$expected,
- \CoreLibs\Create\Hash::__hash($input)
+ \CoreLibs\Create\Hash::__hash($input),
+ '__hash'
+ );
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::hashShort($input),
+ 'hashShort'
);
} else {
$this->assertEquals(
$expected,
- \CoreLibs\Create\Hash::__hash($input, $hash_type)
+ \CoreLibs\Create\Hash::__hash($input, $hash_type),
+ '__hash with hash type'
+ );
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::hash($input, $hash_type),
+ 'hash with hash type'
);
}
}
@@ -193,8 +240,9 @@ final class CoreLibsCreateHashTest extends TestCase
* Undocumented function
*
* @covers ::__hashLong
+ * @covers ::hashLong
* @dataProvider hashLongProvider
- * @testdox __hashLong $input will be $expected [$_dataName]
+ * @testdox __hashLong/hashLong $input will be $expected [$_dataName]
*
* @param string $input
* @param string $expected
@@ -206,6 +254,52 @@ final class CoreLibsCreateHashTest extends TestCase
$expected,
\CoreLibs\Create\Hash::__hashLong($input)
);
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::hashLong($input)
+ );
+ }
+
+ /**
+ * Undocumented function
+ *
+ * @covers ::hash
+ * @covers ::hashStd
+ * @dataProvider hashStandardProvider
+ * @testdox hash/hashStd $input will be $expected [$_dataName]
+ *
+ * @param string $input
+ * @param string $expected
+ * @return void
+ */
+ public function testHashStandard(string $input, string $expected): void
+ {
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::hashStd($input)
+ );
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::hash($input)
+ );
+ }
+
+ /**
+ * Undocumented function
+ *
+ * @covers ::hash
+ * @testdox hash with invalid type [$_dataName]
+ *
+ * @return void
+ */
+ public function testInvalidHashType(): void
+ {
+ $hash_source = 'Some String Text';
+ $expected = hash(\CoreLibs\Create\Hash::STANDARD_HASH, $hash_source);
+ $this->assertEquals(
+ $expected,
+ \CoreLibs\Create\Hash::hash($hash_source, 'DOES_NOT_EXIST')
+ );
}
}
diff --git a/www/admin/class_test.hash.php b/www/admin/class_test.hash.php
index c269a5dc..57da698f 100644
--- a/www/admin/class_test.hash.php
+++ b/www/admin/class_test.hash.php
@@ -38,9 +38,11 @@ print '
' . $PAGE_NAME . '
';
$to_crc = 'Some text block';
// static
-print "S::__CRC32B: $to_crc: " . $hash_class::__crc32b($to_crc) . "
";
-print "S::__SHA1SHORT(off): $to_crc: " . $hash_class::__sha1short($to_crc) . "
";
-print "S::__SHA1SHORT(on): $to_crc: " . $hash_class::__sha1short($to_crc, true) . "
";
+print "S::__CRC32B: $to_crc: " . Hash::__crc32b($to_crc) . "
";
+print "S::__SHA1SHORT(off): $to_crc: " . Hash::__sha1short($to_crc) . "
";
+print "S::hashShort(__sha1Short replace): $to_crc: " . Hash::hashShort($to_crc) . "
";
+print "S::__SHA1SHORT(on): $to_crc: " . Hash::__sha1short($to_crc, true) . "
";
+print "S::sha1Short(__sha1Short replace): $to_crc: " . Hash::sha1Short($to_crc, true) . "
";
print "S::__hash(d): " . $to_crc . "/"
. Hash::STANDARD_HASH_SHORT . ": " . $hash_class::__hash($to_crc) . "
";
foreach (['adler32', 'fnv132', 'fnv1a32', 'joaat', 'sha512'] as $__hash_c) {
@@ -53,13 +55,16 @@ echo "
";
$text = 'Some String Text';
$type = 'crc32b';
print "Hash: " . $type . ": " . hash($type, $text) . "
";
-print "Class: " . $type . ": " . Hash::__hash($text, $type) . "
";
+print "Class (old): " . $type . ": " . Hash::__hash($text, $type) . "
";
+print "Class (new): " . $type . ": " . Hash::hash($text, $type) . "
";
echo "
";
-print "
CURRENT STANDARD_HASH_SHORT: " . Hash::STANDARD_HASH_SHORT . "
";
-print "
CURRENT STANDARD_HASH_LONG: " . Hash::STANDARD_HASH_LONG . "
";
-print "HASH SHORT: " . $to_crc . ": " . Hash::__hash($to_crc) . "
";
-print "HASH LONG: " . $to_crc . ": " . Hash::__hashLong($to_crc) . "
";
+print "CURRENT STANDARD_HASH_SHORT: " . Hash::STANDARD_HASH_SHORT . "
";
+print "CURRENT STANDARD_HASH_LONG: " . Hash::STANDARD_HASH_LONG . "
";
+print "CURRENT STANDARD_HASH: " . Hash::STANDARD_HASH . "
";
+print "HASH SHORT: " . $to_crc . ": " . Hash::hashShort($to_crc) . "
";
+print "HASH LONG: " . $to_crc . ": " . Hash::hashLong($to_crc) . "
";
+print "HASH DEFAULT: " . $to_crc . ": " . Hash::hashStd($to_crc) . "
";
// print "UNIQU ID SHORT : " . Hash::__uniqId() . "
";
// print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "
";
diff --git a/www/lib/CoreLibs/Create/Hash.php b/www/lib/CoreLibs/Create/Hash.php
index bf83afeb..e408f7d3 100644
--- a/www/lib/CoreLibs/Create/Hash.php
+++ b/www/lib/CoreLibs/Create/Hash.php
@@ -10,9 +10,14 @@ namespace CoreLibs\Create;
class Hash
{
+ /** @var string default short hash -> deprecated use STANDARD_HASH_SHORT */
public const DEFAULT_HASH = 'adler32';
+ /** @var string default long hash (40 chars) */
public const STANDARD_HASH_LONG = 'ripemd160';
+ /** @var string default short hash (8 chars) */
public const STANDARD_HASH_SHORT = 'adler32';
+ /** @var string this is the standard hash to use hashStd and hash (64 chars) */
+ public const STANDARD_HASH = 'sha256';
/**
* checks php version and if >=5.2.7 it will flip the string
@@ -20,6 +25,7 @@ class Hash
* hash returns false
* preg_replace fails for older php version
* Use __hash with crc32b or hash('crc32b', ...) for correct output
+ * For future short hashes use hashShort() instead
*
* @param string $string string to crc
* @return string crc32b hash (old type)
@@ -43,19 +49,31 @@ class Hash
* replacement for __crc32b call
*
* @param string $string string to hash
- * @param bool $use_sha use sha instead of crc32b (default false)
+ * @param bool $use_sha use sha1 instead of crc32b (default false)
* @return string hash of the string
+ * @deprecated use __crc32b() for drop in replacement with default, or sha1Short() for use sha true
*/
public static function __sha1Short(string $string, bool $use_sha = false): string
{
if ($use_sha) {
- // return only the first 9 characters
- return substr(hash('sha1', $string), 0, 9);
+ return self::sha1Short($string);
} else {
return self::__crc32b($string);
}
}
+ /**
+ * returns a short sha1
+ *
+ * @param string $string string to hash
+ * @return string hash of the string
+ */
+ public static function sha1Short(string $string): string
+ {
+ // return only the first 9 characters
+ return substr(hash('sha1', $string), 0, 9);
+ }
+
/**
* replacemend for __crc32b call (alternate)
* defaults to adler 32
@@ -65,32 +83,81 @@ class Hash
* @param string $string string to hash
* @param string $hash_type hash type (default adler32)
* @return string hash of the string
+ * @deprecated use hashShort() of short hashes with adler 32 or hash() for other hash types
*/
public static function __hash(
string $string,
- string $hash_type = self::DEFAULT_HASH
+ string $hash_type = self::STANDARD_HASH_SHORT
+ ): string {
+ return self::hash($string, $hash_type);
+ }
+
+ /**
+ * creates a hash over string if any valid hash given.
+ * if no hash type set use sha256
+ *
+ * @param string $string string to ash
+ * @param string $hash_type hash type (default sha256)
+ * @return string hash of the string
+ */
+ public static function hash(
+ string $string,
+ string $hash_type = self::STANDARD_HASH
): string {
- // if not empty, check if in valid list
if (
empty($hash_type) ||
!in_array($hash_type, hash_algos())
) {
// fallback to default hash type if none set or invalid
- $hash_type = self::DEFAULT_HASH;
+ $hash_type = self::STANDARD_HASH;
}
return hash($hash_type, $string);
}
/**
- * Wrapper function for standard long hashd
+ * short hash with max length of 8, uses adler32
+ *
+ * @param string $string string to hash
+ * @return string hash of the string
+ */
+ public static function hashShort(string $string): string
+ {
+ return hash(self::STANDARD_HASH_SHORT, $string);
+ }
+
+ /**
+ * Wrapper function for standard long hash
+ *
+ * @param string $string String to be hashed
+ * @return string Hashed string
+ * @deprecated use hashLong()
+ */
+ public static function __hashLong(string $string): string
+ {
+ return self::hashLong($string);
+ }
+
+ /**
+ * Wrapper function for standard long hash, uses ripmd160
*
* @param string $string String to be hashed
* @return string Hashed string
*/
- public static function __hashLong(string $string): string
+ public static function hashLong(string $string): string
{
return hash(self::STANDARD_HASH_LONG, $string);
}
+
+ /**
+ * create standard hash basd on STANDAR_HASH, currently sha256
+ *
+ * @param string $string string in
+ * @return string hash of the string
+ */
+ public static function hashStd(string $string): string
+ {
+ return self::hash($string, self::STANDARD_HASH);
+ }
}
// __END__