Compare commits
3 Commits
v9.29.0
...
531229e8b7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
531229e8b7 | ||
|
|
d09c20ff9d | ||
|
|
f4ddc5a5fc |
@@ -21,8 +21,10 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
public function hashData(): array
|
public function hashData(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'any string' => [
|
'hash tests' => [
|
||||||
|
// this is the string
|
||||||
'text' => 'Some String Text',
|
'text' => 'Some String Text',
|
||||||
|
// hash list special
|
||||||
'crc32b_reverse' => 'c5c21d91', // crc32b (in revere)
|
'crc32b_reverse' => 'c5c21d91', // crc32b (in revere)
|
||||||
'sha1Short' => '4d2bc9ba0', // sha1Short
|
'sha1Short' => '4d2bc9ba0', // sha1Short
|
||||||
// via hash
|
// via hash
|
||||||
@@ -31,6 +33,8 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
'fnv132' => '9df444f9', // hash: fnv132
|
'fnv132' => '9df444f9', // hash: fnv132
|
||||||
'fnv1a32' => '2c5f91b9', // hash: fnv1a32
|
'fnv1a32' => '2c5f91b9', // hash: fnv1a32
|
||||||
'joaat' => '50dab846', // hash: joaat
|
'joaat' => '50dab846', // hash: joaat
|
||||||
|
'ripemd160' => 'aeae3f041b20136451519edd9361570909300342', // hash: ripemd160,
|
||||||
|
'sha256' => '9055080e022f224fa835929b80582b3c71c672206fa3a49a87412c25d9d42ceb', // hash: sha256
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -81,7 +85,7 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
foreach ($this->hashData() as $name => $values) {
|
foreach ($this->hashData() as $name => $values) {
|
||||||
foreach ([null, 'crc32b', 'adler32', 'fnv132', 'fnv1a32', 'joaat'] as $_hash_type) {
|
foreach ([null, 'crc32b', 'adler32', 'fnv132', 'fnv1a32', 'joaat', 'ripemd160', 'sha256'] as $_hash_type) {
|
||||||
// default value test
|
// default value test
|
||||||
if ($_hash_type === null) {
|
if ($_hash_type === null) {
|
||||||
$hash_type = \CoreLibs\Create\Hash::STANDARD_HASH_SHORT;
|
$hash_type = \CoreLibs\Create\Hash::STANDARD_HASH_SHORT;
|
||||||
@@ -288,7 +292,7 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
* Undocumented function
|
* Undocumented function
|
||||||
*
|
*
|
||||||
* @covers ::hash
|
* @covers ::hash
|
||||||
* @testdox hash with invalid type [$_dataName]
|
* @testdox hash with invalid type
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -301,6 +305,122 @@ final class CoreLibsCreateHashTest extends TestCase
|
|||||||
\CoreLibs\Create\Hash::hash($hash_source, 'DOES_NOT_EXIST')
|
\CoreLibs\Create\Hash::hash($hash_source, 'DOES_NOT_EXIST')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Note: this only tests default sha256
|
||||||
|
*
|
||||||
|
* @covers ::hashHmac
|
||||||
|
* @testdox hash hmac test
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testHashMac(): void
|
||||||
|
{
|
||||||
|
$hash_key = 'FIX KEY';
|
||||||
|
$hash_source = 'Some String Text';
|
||||||
|
$expected = '16479b3ef6fa44e1cdd8b2dcfaadf314d1a7763635e8738f1e7996d714d9b6bf';
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Create\Hash::hashHmac($hash_source, $hash_key)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @covers ::hashHmac
|
||||||
|
* @testdox hash hmac with invalid type
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testInvalidHashMacType(): void
|
||||||
|
{
|
||||||
|
$hash_key = 'FIX KEY';
|
||||||
|
$hash_source = 'Some String Text';
|
||||||
|
$expected = hash_hmac(\CoreLibs\Create\Hash::STANDARD_HASH, $hash_source, $hash_key);
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
\CoreLibs\Create\Hash::hashHmac($hash_source, $hash_key, 'DOES_NOT_EXIST')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @return array<mixed>
|
||||||
|
*/
|
||||||
|
public function providerHashTypes(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'Hash crc32b' => [
|
||||||
|
'crc32b',
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'Hash adler32' => [
|
||||||
|
'adler32',
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'HAsh fnv132' => [
|
||||||
|
'fnv132',
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'Hash fnv1a32' => [
|
||||||
|
'fnv1a32',
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'Hash: joaat' => [
|
||||||
|
'joaat',
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
'Hash: ripemd160' => [
|
||||||
|
'ripemd160',
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'Hash: sha256' => [
|
||||||
|
'sha256',
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
'Hash: invalid' => [
|
||||||
|
'invalid',
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undocumented function
|
||||||
|
*
|
||||||
|
* @covers ::isValidHashType
|
||||||
|
* @covers ::isValidHashHmacType
|
||||||
|
* @dataProvider providerHashTypes
|
||||||
|
* @testdox check if $hash_type is valid for hash $hash_ok and hash hmac $hash_hmac_ok [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $hash_type
|
||||||
|
* @param bool $hash_ok
|
||||||
|
* @param bool $hash_hmac_ok
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testIsValidHashAndHashHmacTypes(string $hash_type, bool $hash_ok, bool $hash_hmac_ok): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
$hash_ok,
|
||||||
|
\CoreLibs\Create\Hash::isValidHashType($hash_type),
|
||||||
|
'hash valid'
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
$hash_hmac_ok,
|
||||||
|
\CoreLibs\Create\Hash::isValidHashHmacType($hash_type),
|
||||||
|
'hash hmac valid'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// __END__
|
// __END__
|
||||||
|
|||||||
125
www/admin/class_test.db.encryption.php
Normal file
125
www/admin/class_test.db.encryption.php
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<?php // phpcs:ignore warning
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phan-file-suppress PhanTypeSuspiciousStringExpression
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
// turn on all error reporting
|
||||||
|
error_reporting(E_ALL | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
|
||||||
|
// basic class test file
|
||||||
|
define('USE_DATABASE', true);
|
||||||
|
// sample config
|
||||||
|
require 'config.php';
|
||||||
|
// define log file id
|
||||||
|
$LOG_FILE_ID = 'classTest-db-query-encryption';
|
||||||
|
ob_end_flush();
|
||||||
|
|
||||||
|
// use CoreLibs\Debug\Support;
|
||||||
|
use CoreLibs\Security\SymmetricEncryption;
|
||||||
|
use CoreLibs\Security\CreateKey;
|
||||||
|
use CoreLibs\Create\Hash;
|
||||||
|
use CoreLibs\Debug\Support;
|
||||||
|
|
||||||
|
$log = new CoreLibs\Logging\Logging([
|
||||||
|
'log_folder' => BASE . LOG,
|
||||||
|
'log_file_id' => $LOG_FILE_ID,
|
||||||
|
'log_per_date' => true,
|
||||||
|
]);
|
||||||
|
// db connection and attach logger
|
||||||
|
$db = new CoreLibs\DB\IO(DB_CONFIG, $log);
|
||||||
|
$db->log->debug('START', '=============================>');
|
||||||
|
|
||||||
|
$PAGE_NAME = 'TEST CLASS: DB QUERY ENCRYPTION';
|
||||||
|
print "<!DOCTYPE html>";
|
||||||
|
print "<html><head><title>" . $PAGE_NAME . "</title></head>";
|
||||||
|
print "<body>";
|
||||||
|
print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||||
|
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||||
|
|
||||||
|
// encryption key
|
||||||
|
$key = CreateKey::generateRandomKey();
|
||||||
|
print "Secret Key: " . $key . "<br>";
|
||||||
|
|
||||||
|
// test text
|
||||||
|
$text_string = "I a some deep secret";
|
||||||
|
//
|
||||||
|
$crypt = new SymmetricEncryption($key);
|
||||||
|
$encrypted = $crypt->encrypt($text_string);
|
||||||
|
$string_hashed = Hash::hashStd($text_string);
|
||||||
|
$string_hmac = Hash::hashHmac($text_string, $key);
|
||||||
|
$decrypted = $crypt->decrypt($encrypted);
|
||||||
|
|
||||||
|
print "String: " . $text_string . "<br>";
|
||||||
|
print "Encrypted: " . $encrypted . "<br>";
|
||||||
|
print "Hashed: " . $string_hashed . "<br>";
|
||||||
|
print "Hmac: " . $string_hmac . "<br>";
|
||||||
|
|
||||||
|
$db->dbExecParams(
|
||||||
|
<<<SQL
|
||||||
|
INSERT INTO test_encryption (
|
||||||
|
-- for compare
|
||||||
|
plain_text,
|
||||||
|
-- via php encryption
|
||||||
|
hash_text, hmac_text, crypt_text,
|
||||||
|
-- -- in DB encryption
|
||||||
|
pg_digest_bytea, pg_digest_text,
|
||||||
|
pg_hmac_bytea, pg_hmac_text,
|
||||||
|
pg_crypt_bytea, pg_crypt_text
|
||||||
|
) VALUES (
|
||||||
|
$1,
|
||||||
|
$2, $3, $4,
|
||||||
|
digest($1::VARCHAR, $5),
|
||||||
|
encode(digest($1, $5), 'hex'),
|
||||||
|
hmac($1, $6, $5),
|
||||||
|
encode(hmac($1, $6, $5), 'hex'),
|
||||||
|
pgp_sym_encrypt($1, $7),
|
||||||
|
encode(pgp_sym_encrypt($1, $7), 'hex')
|
||||||
|
) RETURNING cuuid
|
||||||
|
SQL,
|
||||||
|
[
|
||||||
|
// 1: original string
|
||||||
|
$text_string,
|
||||||
|
// 2: hashed, 3: hmac, 4: encrypted
|
||||||
|
$string_hashed, $string_hmac, $encrypted,
|
||||||
|
// 5: hash type, 6: hmac secret, 7: pgp secret
|
||||||
|
'sha256', $key, $key
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$cuuid = $db->dbGetReturningExt('cuuid');
|
||||||
|
print "INSERTED: $cuuid<br>";
|
||||||
|
print "LAST ERROR: " . $db->dbGetLastError(true) . "<br>";
|
||||||
|
|
||||||
|
// read back
|
||||||
|
$res = $db->dbReturnRowParams(
|
||||||
|
<<<SQL
|
||||||
|
SELECT
|
||||||
|
-- for compare
|
||||||
|
plain_text,
|
||||||
|
-- via php encryption
|
||||||
|
hash_text, hmac_text, crypt_text,
|
||||||
|
-- in DB encryption
|
||||||
|
pg_digest_bytea, pg_digest_text,
|
||||||
|
pg_hmac_bytea, pg_hmac_text,
|
||||||
|
pg_crypt_bytea, pg_crypt_text
|
||||||
|
FROM
|
||||||
|
test_encryption
|
||||||
|
WHERE
|
||||||
|
cuuid = $1
|
||||||
|
SQL,
|
||||||
|
[
|
||||||
|
$cuuid
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
print "RES: <pre>" . Support::prAr($res) . "</pre><br>";
|
||||||
|
|
||||||
|
// do compare
|
||||||
|
|
||||||
|
print "</body></html>";
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -19,6 +19,7 @@ $LOG_FILE_ID = 'classTest-hash';
|
|||||||
ob_end_flush();
|
ob_end_flush();
|
||||||
|
|
||||||
use CoreLibs\Create\Hash;
|
use CoreLibs\Create\Hash;
|
||||||
|
use CoreLibs\Security\CreateKey;
|
||||||
|
|
||||||
$log = new CoreLibs\Logging\Logging([
|
$log = new CoreLibs\Logging\Logging([
|
||||||
'log_folder' => BASE . LOG,
|
'log_folder' => BASE . LOG,
|
||||||
@@ -43,16 +44,24 @@ print "S::__CRC32B: $to_crc: " . Hash::__crc32b($to_crc) . "<br>";
|
|||||||
print "S::hashShort(__sha1Short replace): $to_crc: " . Hash::hashShort($to_crc) . "<br>";
|
print "S::hashShort(__sha1Short replace): $to_crc: " . Hash::hashShort($to_crc) . "<br>";
|
||||||
// print "S::__SHA1SHORT(on): $to_crc: " . Hash::__sha1short($to_crc, true) . "<br>";
|
// print "S::__SHA1SHORT(on): $to_crc: " . Hash::__sha1short($to_crc, true) . "<br>";
|
||||||
print "S::sha1Short(__sha1Short replace): $to_crc: " . Hash::sha1Short($to_crc) . "<br>";
|
print "S::sha1Short(__sha1Short replace): $to_crc: " . Hash::sha1Short($to_crc) . "<br>";
|
||||||
print "S::__hash(d): " . $to_crc . "/"
|
// print "S::__hash(d): " . $to_crc . "/"
|
||||||
. Hash::STANDARD_HASH_SHORT . ": " . $hash_class::__hash($to_crc) . "<br>";
|
// . Hash::STANDARD_HASH_SHORT . ": " . $hash_class::__hash($to_crc) . "<br>";
|
||||||
foreach (['adler32', 'fnv132', 'fnv1a32', 'joaat', 'sha512'] as $__hash_c) {
|
$to_crc_list = [
|
||||||
print "S::__hash($__hash_c): $to_crc: " . $hash_class::__hash($to_crc, $__hash_c) . "<br>";
|
'Some text block',
|
||||||
|
'Some String Text',
|
||||||
|
'any string',
|
||||||
|
];
|
||||||
|
foreach ($to_crc_list as $__to_crc) {
|
||||||
|
foreach (['adler32', 'fnv132', 'fnv1a32', 'joaat', 'ripemd160', 'sha256', 'sha512'] as $__hash_c) {
|
||||||
|
print "Hash::hash($__hash_c): $__to_crc: " . Hash::hash($to_crc, $__hash_c) . "<br>";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// static use
|
// static use
|
||||||
print "U-S::__CRC32B: $to_crc: " . Hash::__crc32b($to_crc) . "<br>";
|
print "U-S::__CRC32B: $to_crc: " . Hash::__crc32b($to_crc) . "<br>";
|
||||||
|
|
||||||
echo "<hr>";
|
echo "<hr>";
|
||||||
$text = 'Some String Text';
|
$text = 'Some String Text';
|
||||||
|
// $text = 'any string';
|
||||||
$type = 'crc32b';
|
$type = 'crc32b';
|
||||||
print "Hash: " . $type . ": " . hash($type, $text) . "<br>";
|
print "Hash: " . $type . ": " . hash($type, $text) . "<br>";
|
||||||
// print "Class (old): " . $type . ": " . Hash::__hash($text, $type) . "<br>";
|
// print "Class (old): " . $type . ": " . Hash::__hash($text, $type) . "<br>";
|
||||||
@@ -66,6 +75,31 @@ print "HASH SHORT: " . $to_crc . ": " . Hash::hashShort($to_crc) . "<br>";
|
|||||||
print "HASH LONG: " . $to_crc . ": " . Hash::hashLong($to_crc) . "<br>";
|
print "HASH LONG: " . $to_crc . ": " . Hash::hashLong($to_crc) . "<br>";
|
||||||
print "HASH DEFAULT: " . $to_crc . ": " . Hash::hashStd($to_crc) . "<br>";
|
print "HASH DEFAULT: " . $to_crc . ": " . Hash::hashStd($to_crc) . "<br>";
|
||||||
|
|
||||||
|
echo "<hr>";
|
||||||
|
$key = CreateKey::generateRandomKey();
|
||||||
|
$key = "FIX KEY";
|
||||||
|
print "Secret Key: " . $key . "<br>";
|
||||||
|
print "HASHMAC DEFAULT (fix): " . $to_crc . ": " . Hash::hashHmac($to_crc, $key) . "<br>";
|
||||||
|
$key = CreateKey::generateRandomKey();
|
||||||
|
print "Secret Key: " . $key . "<br>";
|
||||||
|
print "HASHMAC DEFAULT (random): " . $to_crc . ": " . Hash::hashHmac($to_crc, $key) . "<br>";
|
||||||
|
|
||||||
|
echo "<hr>";
|
||||||
|
$hash_types = ['crc32b', 'sha256', 'invalid'];
|
||||||
|
foreach ($hash_types as $hash_type) {
|
||||||
|
echo "<b>Checking $hash_type:</b><br>";
|
||||||
|
if (Hash::isValidHashType($hash_type)) {
|
||||||
|
echo "hash type: $hash_type is valid<br>";
|
||||||
|
} else {
|
||||||
|
echo "hash type: $hash_type is INVALID<br>";
|
||||||
|
}
|
||||||
|
if (Hash::isValidHashHmacType($hash_type)) {
|
||||||
|
echo "hash hmac type: $hash_type is valid<br>";
|
||||||
|
} else {
|
||||||
|
echo "hash hmac type: $hash_type is INVALID<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// print "UNIQU ID SHORT : " . Hash::__uniqId() . "<br>";
|
// print "UNIQU ID SHORT : " . Hash::__uniqId() . "<br>";
|
||||||
// print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "<br>";
|
// print "UNIQU ID LONG : " . Hash::__uniqIdLong() . "<br>";
|
||||||
|
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ $test_files = [
|
|||||||
'class_test.db.dbReturn.php' => 'Class Test: DB dbReturn',
|
'class_test.db.dbReturn.php' => 'Class Test: DB dbReturn',
|
||||||
'class_test.db.single.php' => 'Class Test: DB single query tests',
|
'class_test.db.single.php' => 'Class Test: DB single query tests',
|
||||||
'class_test.db.convert-placeholder.php' => 'Class Test: DB convert placeholder',
|
'class_test.db.convert-placeholder.php' => 'Class Test: DB convert placeholder',
|
||||||
|
'class_test.db.encryption.php' => 'Class Test: DB pgcrypto',
|
||||||
'class_test.convert.colors.php' => 'Class Test: CONVERT COLORS',
|
'class_test.convert.colors.php' => 'Class Test: CONVERT COLORS',
|
||||||
'class_test.check.colors.php' => 'Class Test: CHECK COLORS',
|
'class_test.check.colors.php' => 'Class Test: CHECK COLORS',
|
||||||
'class_test.mime.php' => 'Class Test: MIME',
|
'class_test.mime.php' => 'Class Test: MIME',
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ 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 sha1 instead of crc32b (default false)
|
* @param bool $use_sha [default=false] use sha1 instead of crc32b
|
||||||
* @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
|
* @deprecated use __crc32b() for drop in replacement with default, or sha1Short() for use sha true
|
||||||
*/
|
*/
|
||||||
@@ -81,7 +81,7 @@ class Hash
|
|||||||
* all that create 8 char long hashes
|
* all that create 8 char long hashes
|
||||||
*
|
*
|
||||||
* @param string $string string to hash
|
* @param string $string string to hash
|
||||||
* @param string $hash_type hash type (default adler32)
|
* @param string $hash_type [default=STANDARD_HASH_SHORT] 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
|
* @deprecated use hashShort() of short hashes with adler 32 or hash() for other hash types
|
||||||
*/
|
*/
|
||||||
@@ -92,12 +92,40 @@ class Hash
|
|||||||
return self::hash($string, $hash_type);
|
return self::hash($string, $hash_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if hash type is valid, returns false if not
|
||||||
|
*
|
||||||
|
* @param string $hash_type
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function isValidHashType(string $hash_type): bool
|
||||||
|
{
|
||||||
|
if (!in_array($hash_type, hash_algos())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if hash hmac type is valid, returns false if not
|
||||||
|
*
|
||||||
|
* @param string $hash_hmac_type
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function isValidHashHmacType(string $hash_hmac_type): bool
|
||||||
|
{
|
||||||
|
if (!in_array($hash_hmac_type, hash_hmac_algos())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a hash over string if any valid hash given.
|
* creates a hash over string if any valid hash given.
|
||||||
* if no hash type set use sha256
|
* if no hash type set use sha256
|
||||||
*
|
*
|
||||||
* @param string $string string to ash
|
* @param string $string string to hash
|
||||||
* @param string $hash_type hash type (default sha256)
|
* @param string $hash_type [default=STANDARD_HASH] hash type (default sha256)
|
||||||
* @return string hash of the string
|
* @return string hash of the string
|
||||||
*/
|
*/
|
||||||
public static function hash(
|
public static function hash(
|
||||||
@@ -108,12 +136,36 @@ class Hash
|
|||||||
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 empty or invalid
|
||||||
$hash_type = self::STANDARD_HASH;
|
$hash_type = self::STANDARD_HASH;
|
||||||
}
|
}
|
||||||
return hash($hash_type, $string);
|
return hash($hash_type, $string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a hash mac key
|
||||||
|
*
|
||||||
|
* @param string $string string to hash mac
|
||||||
|
* @param string $key key to use
|
||||||
|
* @param string $hash_type [default=STANDARD_HASH]
|
||||||
|
* @return string hash mac string
|
||||||
|
*/
|
||||||
|
public static function hashHmac(
|
||||||
|
string $string,
|
||||||
|
#[\SensitiveParameter]
|
||||||
|
string $key,
|
||||||
|
string $hash_type = self::STANDARD_HASH
|
||||||
|
): string {
|
||||||
|
if (
|
||||||
|
empty($hash_type) ||
|
||||||
|
!in_array($hash_type, hash_hmac_algos())
|
||||||
|
) {
|
||||||
|
// fallback to default hash type if e or invalid
|
||||||
|
$hash_type = self::STANDARD_HASH;
|
||||||
|
}
|
||||||
|
return hash_hmac($hash_type, $string, $key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* short hash with max length of 8, uses adler32
|
* short hash with max length of 8, uses adler32
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user