Class Hash update
This commit is contained in:
@@ -22,6 +22,9 @@ parameters:
|
|||||||
# - vendor
|
# - vendor
|
||||||
# ignore errores with
|
# ignore errores with
|
||||||
ignoreErrors:
|
ignoreErrors:
|
||||||
|
-
|
||||||
|
message: '#Expression in empty\(\) is not falsy.#'
|
||||||
|
path: %currentWorkingDirectory%/src/Language/GetLocale.php
|
||||||
#- # this error is ignore because of the PHP 8.0 to 8.1 change for pg_*, only for 8.0 or lower
|
#- # this error is ignore because of the PHP 8.0 to 8.1 change for pg_*, only for 8.0 or lower
|
||||||
# message: "#^Parameter \\#1 \\$(result|connection) of function pg_\\w+ expects resource(\\|null)?, object\\|resource(\\|bool)? given\\.$#"
|
# message: "#^Parameter \\#1 \\$(result|connection) of function pg_\\w+ expects resource(\\|null)?, object\\|resource(\\|bool)? given\\.$#"
|
||||||
# path: %currentWorkingDirectory%/www/lib/CoreLibs/DB/SQL/PgSQL.php
|
# path: %currentWorkingDirectory%/www/lib/CoreLibs/DB/SQL/PgSQL.php
|
||||||
|
|||||||
@@ -1024,8 +1024,12 @@ class Basic
|
|||||||
*/
|
*/
|
||||||
public function __sha1Short(string $string, bool $use_sha = false): string
|
public function __sha1Short(string $string, bool $use_sha = false): string
|
||||||
{
|
{
|
||||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::__sha1Short()', E_USER_DEPRECATED);
|
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::sha1Short() or ::__crc32b()', E_USER_DEPRECATED);
|
||||||
return \CoreLibs\Create\Hash::__sha1Short($string, $use_sha);
|
if ($use_sha) {
|
||||||
|
return \CoreLibs\Create\Hash::sha1Short($string);
|
||||||
|
} else {
|
||||||
|
return \CoreLibs\Create\Hash::__crc32b($string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1040,8 +1044,8 @@ class Basic
|
|||||||
*/
|
*/
|
||||||
public function __hash(string $string, string $hash_type = 'adler32'): string
|
public function __hash(string $string, string $hash_type = 'adler32'): string
|
||||||
{
|
{
|
||||||
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::__hash()', E_USER_DEPRECATED);
|
trigger_error('Method ' . __METHOD__ . ' is deprecated, use \CoreLibs\Create\Hash::hash()', E_USER_DEPRECATED);
|
||||||
return \CoreLibs\Create\Hash::__hash($string, $hash_type);
|
return \CoreLibs\Create\Hash::hash($string, $hash_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
// *** HASH FUNCTIONS END
|
// *** HASH FUNCTIONS END
|
||||||
|
|||||||
@@ -10,9 +10,14 @@ namespace CoreLibs\Create;
|
|||||||
|
|
||||||
class Hash
|
class Hash
|
||||||
{
|
{
|
||||||
|
/** @var string default short hash -> deprecated use STANDARD_HASH_SHORT */
|
||||||
public const DEFAULT_HASH = 'adler32';
|
public const DEFAULT_HASH = 'adler32';
|
||||||
|
/** @var string default long hash (40 chars) */
|
||||||
public const STANDARD_HASH_LONG = 'ripemd160';
|
public const STANDARD_HASH_LONG = 'ripemd160';
|
||||||
|
/** @var string default short hash (8 chars) */
|
||||||
public const STANDARD_HASH_SHORT = 'adler32';
|
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
|
* checks php version and if >=5.2.7 it will flip the string
|
||||||
@@ -20,6 +25,7 @@ class Hash
|
|||||||
* hash returns false
|
* hash returns false
|
||||||
* preg_replace fails for older php version
|
* preg_replace fails for older php version
|
||||||
* Use __hash with crc32b or hash('crc32b', ...) for correct output
|
* Use __hash with crc32b or hash('crc32b', ...) for correct output
|
||||||
|
* For future short hashes use hashShort() instead
|
||||||
*
|
*
|
||||||
* @param string $string string to crc
|
* @param string $string string to crc
|
||||||
* @return string crc32b hash (old type)
|
* @return string crc32b hash (old type)
|
||||||
@@ -43,19 +49,31 @@ class Hash
|
|||||||
* replacement for __crc32b call
|
* replacement for __crc32b call
|
||||||
*
|
*
|
||||||
* @param string $string string to hash
|
* @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
|
* @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
|
public static function __sha1Short(string $string, bool $use_sha = false): string
|
||||||
{
|
{
|
||||||
if ($use_sha) {
|
if ($use_sha) {
|
||||||
// return only the first 9 characters
|
return self::sha1Short($string);
|
||||||
return substr(hash('sha1', $string), 0, 9);
|
|
||||||
} else {
|
} else {
|
||||||
return self::__crc32b($string);
|
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)
|
* replacemend for __crc32b call (alternate)
|
||||||
* defaults to adler 32
|
* defaults to adler 32
|
||||||
@@ -65,32 +83,81 @@ class Hash
|
|||||||
* @param string $string string to hash
|
* @param string $string string to hash
|
||||||
* @param string $hash_type hash type (default adler32)
|
* @param string $hash_type hash type (default adler32)
|
||||||
* @return string hash of the string
|
* @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(
|
public static function __hash(
|
||||||
string $string,
|
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 {
|
): string {
|
||||||
// if not empty, check if in valid list
|
|
||||||
if (
|
if (
|
||||||
empty($hash_type) ||
|
empty($hash_type) ||
|
||||||
!in_array($hash_type, hash_algos())
|
!in_array($hash_type, hash_algos())
|
||||||
) {
|
) {
|
||||||
// fallback to default hash type if none set or invalid
|
// 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);
|
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
|
* @param string $string String to be hashed
|
||||||
* @return string Hashed string
|
* @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);
|
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__
|
// __END__
|
||||||
|
|||||||
@@ -4056,7 +4056,7 @@ class IO
|
|||||||
*/
|
*/
|
||||||
public function dbGetQueryHash(string $query, array $params = []): string
|
public function dbGetQueryHash(string $query, array $params = []): string
|
||||||
{
|
{
|
||||||
return Hash::__hashLong(
|
return Hash::hashLong(
|
||||||
$query . (
|
$query . (
|
||||||
$params !== [] ?
|
$params !== [] ?
|
||||||
'#' . json_encode($params) : ''
|
'#' . json_encode($params) : ''
|
||||||
|
|||||||
@@ -183,8 +183,9 @@ list($HOST_NAME) = array_pad(explode(':', $_SERVER['HTTP_HOST'], 2), 2, null);
|
|||||||
define('HOST_NAME', $HOST_NAME);
|
define('HOST_NAME', $HOST_NAME);
|
||||||
// BAIL ON MISSING MASTER SITE CONFIG
|
// BAIL ON MISSING MASTER SITE CONFIG
|
||||||
if (!isset($SITE_CONFIG[HOST_NAME]['location'])) {
|
if (!isset($SITE_CONFIG[HOST_NAME]['location'])) {
|
||||||
echo 'Missing SITE_CONFIG entry for: "' . HOST_NAME . '". Contact Administrator';
|
throw new \InvalidArgumentException(
|
||||||
exit;
|
'Missing SITE_CONFIG entry for: "' . HOST_NAME . '". Contact Administrator'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// BAIL ON MISSING DB CONFIG:
|
// BAIL ON MISSING DB CONFIG:
|
||||||
// we have either no db selction for this host but have db config entries
|
// we have either no db selction for this host but have db config entries
|
||||||
@@ -200,8 +201,9 @@ if (
|
|||||||
empty($DB_CONFIG[$SITE_CONFIG[HOST_NAME]['db_host']]))
|
empty($DB_CONFIG[$SITE_CONFIG[HOST_NAME]['db_host']]))
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
echo 'No matching DB config found for: "' . HOST_NAME . '". Contact Administrator';
|
throw new \InvalidArgumentException(
|
||||||
exit;
|
'No matching DB config found for: "' . HOST_NAME . '". Contact Administrator'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// set SSL on
|
// set SSL on
|
||||||
$is_secure = false;
|
$is_secure = false;
|
||||||
|
|||||||
@@ -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
|
* Undocumented function
|
||||||
*
|
*
|
||||||
@@ -136,9 +152,13 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
|
* phpcs:disable Generic.Files.LineLength
|
||||||
* @covers ::__sha1Short
|
* @covers ::__sha1Short
|
||||||
|
* @covers ::__crc32b
|
||||||
|
* @covers ::sha1Short
|
||||||
* @dataProvider sha1ShortProvider
|
* @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 $input
|
||||||
* @param string $expected
|
* @param string $expected
|
||||||
@@ -149,16 +169,29 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
// uses crc32b
|
// uses crc32b
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$expected,
|
||||||
\CoreLibs\Create\Hash::__sha1Short($input)
|
\CoreLibs\Create\Hash::__sha1Short($input),
|
||||||
|
'__sha1Short depreacted'
|
||||||
);
|
);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$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
|
// sha1 type
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected_sha1,
|
$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
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::__hash
|
* @covers ::__hash
|
||||||
|
* @covers ::hashShort
|
||||||
|
* @covers ::hashShort
|
||||||
* @dataProvider hashProvider
|
* @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 $input
|
||||||
* @param string|null $hash_type
|
* @param string|null $hash_type
|
||||||
@@ -179,12 +214,24 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
if ($hash_type === null) {
|
if ($hash_type === null) {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$expected,
|
||||||
\CoreLibs\Create\Hash::__hash($input)
|
\CoreLibs\Create\Hash::__hash($input),
|
||||||
|
'__hash'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Create\Hash::hashShort($input),
|
||||||
|
'hashShort'
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$expected,
|
$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
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::__hashLong
|
* @covers ::__hashLong
|
||||||
|
* @covers ::hashLong
|
||||||
* @dataProvider hashLongProvider
|
* @dataProvider hashLongProvider
|
||||||
* @testdox __hashLong $input will be $expected [$_dataName]
|
* @testdox __hashLong/hashLong $input will be $expected [$_dataName]
|
||||||
*
|
*
|
||||||
* @param string $input
|
* @param string $input
|
||||||
* @param string $expected
|
* @param string $expected
|
||||||
@@ -206,6 +254,52 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
$expected,
|
$expected,
|
||||||
\CoreLibs\Create\Hash::__hashLong($input)
|
\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')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user